Sorry about the maybe misleading title, but I wonder why is this approach of creating objects and adding properties and methods:
//A)
var myObjCreatedByFunction = function() {
this.prop1 = "a";
this.func1 = function() {
alert("a");
}
};
and then using prototype to create additional methods
myObjCreatedByFunction.prototype.func2 = function() { ... }
preferred over simple and plain creating object:
//B)
var myObj = {
prop1: "a",
func1: function() {
alert("a");
}
};
and just adding new method by
myObj.func2 = function() { ... }
as I see some skilled developers use the first approach and use prototypes even if they never create new instances later.
Furthermore, is the use of IIFE preferred over both examples and if so, why? (I see a lot of this usage lately)
var myObjIIFE = (function() {
return {
prop1: "a",
func1: function() {
alert("a");
}
}
})());
Can someone explain please when should I use first approach, second and third and why? Thank you!
Aucun commentaire:
Enregistrer un commentaire