javascript naming a function in an object
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"() return 1;
"test": function () return 1;
It's an ES6 new feature. See comparison here and here
– Yong Quan
Aug 30 at 8:59
3 Answers
3
"test": function () return 1;
is the old way and "test"() return 1;
the new way with the function
keyword being ommited.
"test": function () return 1;
"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
Wow... Old vs new way is great.
– Praveen Kumar Purushothaman
Aug 30 at 8:59
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. (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.)
name: function() ...
name: function() ...
Gives you the freedom of having private functions only used by your
module (such as my internalSomething
above). 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
outside the scoping function.
internalSomething
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.).
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Or
"test"() return 1;
. It’s not exactly the same as"test": function () return 1;
, as the short-hand syntax creates non-constructible methods, rather than constructible functions.– Xufox
Aug 30 at 8:58