JS中奇怪的函数定义语法
Weird function definition syntax in JS
我在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?
这是一个ES6语法糖。
示例:
var a = {
foo: 'bar',
log() {
console.log('hi');
}
}
a.foo // 'bar'
a.log() // 'hi'
就像你做了log: function { console.log('hi') }
一样
相关文章:
- 用嵌套函数和默认函数定义函数
- 使用Extjs进行函数定义
- JS中奇怪的函数定义语法
- 重写javascript函数定义
- 使用角度图时,我可以用函数定义数据点颜色吗
- modal.js中的匿名函数定义
- 为从AJAX调用中utlizes DATA参数的函数定义回调
- Emacs:在 etag 中查找函数定义
- Javascript:对象函数定义
- 为什么要在函数定义之外引用静态变量(函数属性)
- Ember 对象函数定义中的计算属性.js
- javascript/jQuery中函数定义的区别
- CoffeeScript 承诺与函数定义链接
- 如何在使用 eval() 评估 JavaScript 时将回调函数定义作为参数传递
- Javascript - 为什么下面的函数定义错误
- 使用函数声明时的自定义函数(也称为惰性函数定义)
- JavaScript 函数定义不正确
- jQuery off on 时是用匿名函数定义的
- 停止 IntelliJ 11 在启用 JSLint 时向每个嵌套函数定义添加“use strict”
- 为什么不是't我为这个函数定义的javascript函数