This my code
const x = {
a() {
console.log("a");
},
b: function() {
console.log("b");
},
};
Is there a difference between a
& b
functions?
I read somewhere, that the a() {}
way of creating methods is shorter, but can bring a number of problems.
Use it only in those cases when you are sure that you will never use recursion or pass a function to events handlers.
Is this true?
The only real difference is that
a
is defined as a method, meaning that it can't be instantiated withnew
:A trivial difference is that, in sloppy mode, the method has the
arguments
andcaller
properties on its internal prototype, whereas the function has those properties on the function object itself:Accessing these properties is forbidden in strict mode, and in method definitions. They are deprecated.