What is the difference between method1 and method2 ?
let myObject = {
method1: function(x){
console.log(...);
},
method2(x){
console.log(...);
}
}
2 Answers 2
There is no difference. method2 is just a shorthand syntax introduced in ES6.
Starting with ECMAScript 2015, a shorter syntax for method definitions on objects initializers is introduced. It is a shorthand for a function assigned to the method's name.
Source: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Functions/Method_definitions
Comments
There is no real difference here, as stated in the comments.
It's just a matter of "how do you want to write it".
But if you're starting a project, you should choose one way and stick to it, and if the project is already started, try to stick to the way the most used. It'll greatly help to read the code easily.
Also, if it's about scope or binding, you should have a look here :
method1.