这个对象方法定义如何在没有“函数”的情况下工作。关键词? [英] How does this object method definition work without the "function" keyword?

查看:124
本文介绍了这个对象方法定义如何在没有“函数”的情况下工作。关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这是因为不小心离开函数关键字。通常以下模块中的 foobar 方法将被声明为 foobar:function(arg1),但有趣的是以下作品,至少在某些浏览器中,例如Chrome版本44.0.2403.157 m,但它在IE 11.0.9600.17959中失败

I discovered this by accidentally leaving off the function keyword. Ordinarily the foobar method in the module below would be declared as foobar: function(arg1), but interestingly the following works, at least in some browsers, e.g. Chrome Version 44.0.2403.157 m, but it fails in IE 11.0.9600.17959

在任何浏览器中这可能运行的可能性如何?这是一种新的ES6功能吗?

How is it possible that this runs at all in any browser? Is this some sort of new ES6 functionality?

var module = {
    foobar(arg1) {
        alert(arg1);
    }
};

module.foobar("Hello World");


推荐答案


这在任何浏览器中都运行?是否是某种新的ES6功能?

How is it possible that this runs at all in any browser? Is is some sort of new ES6 functionality?


...

对象的属性还可以引用函数或getter或
setter方法。

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) {},
};

在ECMAScript 6中,有一个简写符号可用,因此
关键字function不再需要。

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() {}
};

...

这篇关于这个对象方法定义如何在没有“函数”的情况下工作。关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆