简体   繁体   English

javascript在对象中命名函数

[英]javascript naming a function in an object

Which one should I use between these two, is there one that has an advantage over the other? 在这两者之间,我应该使用哪一个?

// 1st
{ ["test"]() { return 1; } }

// 2nd
{ "test": function () { return 1; } }

"test": function () { return 1; } "test": function () { return 1; } is the old way and "test"() { return 1; } "test": function () { return 1; }是旧方法, "test"() { return 1; } "test"() { return 1; } the new way with the function keyword being ommited. "test"() { return 1; }省略了function关键字的新方法。

Also note the [] here allow you to use a variable as identifier 另请注意,这里的[]允许您使用变量作为标识符

 let name = "test1" let a = { "test2": function () { return "you called test2" }, "test3"() { return "you called test3" }, [name]() { return "you called test1" }, [name + "1"]() { return "you called " + name + "1" } } // written differently works the same console.log( a.test2() ) // "you called test2" <-- the old way declared one console.log( a.test3() ) // "you called test3" <-- new way declared one // same call console.log( a[name]() ) // "you called test1" <-- declared using console.log( a.test1() ) // "you called test1" <-- [name]() {...} // the [...] notation allow the build of identifier freely console.log( a.test11() ) // "you called test11" <-- declared using console.log( a[name + "1"]() ) // "you called test11" <-- [name + "1"]() {...} 

since Javascript like many other language tend to avoid deprecation for old program to continue working, you get to a point where one thing can be done many way 由于Javascript像其他许多语言一样倾向于避免过时而导致旧程序无法继续运行,因此您会发现一件事情可以通过多种方式完成

It allows to use variable property names: 它允许使用变量属性名称:

 let propName = "test" console.log({ [propName]() { return 1 } }.test()); 

Advantages: 好处:

  • Functions are declared normally and therefore have names. 函数通常是声明的,因此具有名称。 (Whereas with the {name: function() { ... }} format, all of your functions are anonymous, even though the properties referencing them have names.) Names help tools help you, from showing call stacks in a debugger, to telling you what function threw an exception. (尽管使用{name: function() { ... }}格式,即使引用它们的属性都有名称,您的所有函数也是匿名的。)名称帮助工具可以帮助您,从在调试器中显示调用堆栈,到告诉您什么函数引发了异常。 (2015 Update: The latest JavaScript specification, ECMAScript 6th edition, defines a large number of ways the JavaScript engine must infer a function's name. One of those is when the function is assigned to a property as in our {name: function() { ... }} example. So as engines implement ES6, this reason will go away.) (2015年更新:最新的JavaScript规范ECMAScript第6版定义了JavaScript引擎必须推断函数名称的多种方法。其中之一是,如我们的{name: function() { ... }}示例。因此,当引擎实施ES6时,这个原因将消失。)

  • Gives you the freedom of having private functions only used by your module (such as my internalSomething above). 使您可以自由使用仅由您的模块使用的私有函数(例如上面的我的internalSomething )。 No other code on the page can call those functions; 页面上没有其他代码可以调用这些函数。 they're truly private. 他们是真正的私人。 Only the ones you export at the end, in the return statement, are visible 在return语句中,只有最后导出的内容可见
    outside the scoping function. 范围界定功能之外。

  • Makes it easy to return different functions depending on environment, if the implementation just changes completely (such as IE-vs-W3C stuff, or SVG vs. Canvas, etc.). 如果实现只是完全改变(例如IE-vs-W3C或SVG vs. Canvas等),则可以轻松地根据环境返回不同的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM