What is the difference between below approaches for adding methods to an object:
// Appending methods to a function using nested functions
var myObj1 = {
myMethod : function() {
console.log('myObj1.myMethod was called');
},
myOtherMethod : function() {
},
myOtherOtherMethod : function() {
}
}
// Appending methods to a function using the dot operator:
var myObj2 = {};
myObj2.myMethod = function(){
console.log('myObj2.myMethod was called');
}
myObj2.myOtherMethod = function(){
}
myObj2.myOtherOtherMethod = function(){
}
myObj1.myMethod(); // myObj1.myMethod was called
myObj2.myMethod(); // myObj2.myMethod was called
Both do the same thing. Besides the different syntax, is one approach preferred over the other? From my point of view, both approaches simply add methods (or function if you like) to an object.
</p>