我在Meteor.js教程中发现了这段代码。是ES2015:
Meteor.methods({
'tasks.insert'(text) {
check(text, String);
// Make sure the user is logged in before inserting a task
if (! this.userId) {
throw new Meteor.Error('not-authorized');
}
Tasks.insert({
text,
createdAt: new Date(),
owner: this.userId,
username: Meteor.users.findOne(this.userId).username,
});
},
});我对函数的定义方式很好奇。正如我们所看到的,Meteor.methods被赋予一个对象作为参数,并且该对象包含函数作为它的道具值。但这是多么可怕的事情:
'tasks.insert'(text) {??我希望'tasks.insert‘是一个表示道具名称的字符串,这个道具名应该映射到一个执行插入的函数。但为什么不像
'tasks.insert': (text) => {或
'tasks.insert': function(text) {这是什么模式,这怎么可能是一个有效的JS?
发布于 2016-05-08 16:28:28
这是一个ES6 合成糖。
示例:
var a = {
foo: 'bar',
log() {
console.log('hi');
}
}
a.foo // 'bar'
a.log() // 'hi'就像你做了log: function { console.log('hi') }一样
https://stackoverflow.com/questions/37102098
复制相似问题