I came across a particular way of placing a function inside of a javascript object I don't quite understand. Typically, you would have something like:
var obj = {
foo: function() {
return 'bar';
}
} //obj.foo() === 'bar'
However, I found that I can get the same thing with:
var obj = {
foo() {
return 'bar';
}
} //obj.foo() === 'bar'
Is this just another way of declaring methods?
This is an ES2015 feature regarding to Method Definition.
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.
Check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With