JS中的这种符号是什么,有人可以解释一下

pi2*_*018 2 javascript ecmascript-6

我遇到了这个函数,名为generateMessage,它接受2个参数并返回和对象.功能如下:

var generateMessage = (from, text)=>{
    return {
        from,
         text,
        createdAt: new Date().getTime()
    }
};

module.exports = {generateMessage};
Run Code Online (Sandbox Code Playgroud)

但这抛出任何错误,并附加3个属性来返回的对象:"从","的.text"和".createdAt",我感到困惑的"从"和"的.text"属性.

我的问题是为什么我们不写from: from,text:text以这种方式返回的对象将具有.from和.text的proto属性,它们的值将作为参数fromtext来自参数.

为什么只写fromtext返回的对象在这种情况下工作?

小智 5

那是ECMAScript的'速记'的属性和符号:

http://es6-features.org/#PropertyShorthand

http://es6-features.org/#ObjectMatchingShorthandNotation

顾名思义,它就是对象定义的简写方法.