javascript - 如何返回与局部变量具有相同属性名称和值的对象
问题描述
有谁知道我将如何返回值x
,y
和w
?h
下面的代码不起作用,但这是我试图实现的一个例子
const selectorRect = (function() {
let x = undefined;
let y = undefined;
let w = undefined;
let h = undefined;
return {
x: x,
y: y,
w: w,
h: h
};
})();
console.log(selectorRect);
解决方案
ES6 允许您使用速记属性值。
const selectorRect = () => {
let x = undefined;
let y = undefined;
let w = undefined;
let h = undefined;
return { x, y, w, h }
}
console.log(selectorRect())
推荐阅读
- sql - Knex + Postgresql + node - 如何为选定的列调用 postgresql 函数
- tensorflow - 计算TensorFlow中可变序列长度的实际序列长度?
- excel - 导出第 1 行中带有引号分隔符的文本文件的过程 - Excel
- c# - 如何在 swashbuckl asp.net core web api 上添加自定义标头
- jquery - 如何申请和取消折扣
- php - MySQL JSON 数据类型更新与删除
- api - APIGEE 修订比较
- angular - 如何在 Angular 组件 + webpack 中预处理并正确包含 scss
- jenkins - CI/CD 管道中的工作流程 - Jenkins、Kubernetes 和 SVN/GitHub 代码库
- android - 动画 GIF 无法播放