首页 > 解决方案 > 如何返回与局部变量具有相同属性名称和值的对象

问题描述

有谁知道我将如何返回值x,ywh下面的代码不起作用,但这是我试图实现的一个例子

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);

标签: javascript

解决方案


ES6 允许您使用速记属性值。

const selectorRect = () => {
  let x = undefined;
  let y = undefined;
  let w = undefined;
  let h = undefined;
  return { x, y, w, h }
}

console.log(selectorRect())

推荐阅读