我遇到了这个名为generateMessage的函数,它接受2个参数并返回一个对象。功能如下:
var generateMessage = (from, text)=>{
return {
from,
text,
createdAt: new Date().getTime()
}
};
module.exports = {generateMessage};
这不会抛出任何错误,并将 3 个属性附加到返回的对象: '.from' 、 '.text' 和 '.createdAt',我对 '.from' 感到困惑和“.text”属性。
我的问题是我们为什么不写 from: from
, text:text
,这样返回的对象将具有 .from 和 .text 的 proto 属性,其值为 from
和text
从参数来看。
为什么只写 from
和text
在这种情况下返回的对象起作用吗?
最佳答案
这是 ECMAScript 的“简写”属性和表示法:
http://es6-features.org/#PropertyShorthand
http://es6-features.org/#ObjectMatchingShorthandNotation
顾名思义,对象定义的一种简写方法。
关于javascript - JS中这个表示法是什么,有人可以解释一下吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51866641/