如果没有 "function" 关键字,这个对象方法定义如何工作?
How does this object method definition work without the "function" keyword?
我不小心漏掉了 function
关键字发现了这一点。通常下面模块中的 foobar
方法将被声明为 foobar: function(arg1)
,但有趣的是,至少在某些浏览器中,例如Chrome 版本 44.0.2403.157 m,但在 IE 11.0.9600.17959 中失败
这怎么可能在任何浏览器中运行?这是某种新的 ES6 功能吗?
var module = {
foobar(arg1) {
alert(arg1);
}
};
module.foobar("Hello World");
How is it possible that this runs at all in any browser? Is is some sort of new ES6 functionality?
Yes.
...
Method definitions
A property of an object can also refer to a function or a getter or
setter method.
var o = {
property: function ([parameters]) {},
get property() {},
set property(value) {},
};
In ECMAScript 6, a shorthand notation is available, so that the
keyword "function" is no longer necessary.
// Shorthand method names (ES6)
var o = {
property([parameters]) {},
get property() {},
set property(value) {},
* generator() {}
};
...
ES6 允许 "concise methods",正如您所发现的,它还不跨浏览器兼容。
我不小心漏掉了 function
关键字发现了这一点。通常下面模块中的 foobar
方法将被声明为 foobar: function(arg1)
,但有趣的是,至少在某些浏览器中,例如Chrome 版本 44.0.2403.157 m,但在 IE 11.0.9600.17959 中失败
这怎么可能在任何浏览器中运行?这是某种新的 ES6 功能吗?
var module = {
foobar(arg1) {
alert(arg1);
}
};
module.foobar("Hello World");
How is it possible that this runs at all in any browser? Is is some sort of new ES6 functionality?
Yes.
...
Method definitions
A property of an object can also refer to a function or a getter or setter method.
var o = { property: function ([parameters]) {}, get property() {}, set property(value) {}, };
In ECMAScript 6, a shorthand notation is available, so that the keyword "function" is no longer necessary.
// Shorthand method names (ES6) var o = { property([parameters]) {}, get property() {}, set property(value) {}, * generator() {} };
...
ES6 允许 "concise methods",正如您所发现的,它还不跨浏览器兼容。