I find that in Javascript there are many methods of creating an object so what would be the appropriate approach for constructing an object and why.
parent = {
init: function(option){
if (option == 1)
_.extends(this, sub1)
else
_.extends(this, sub2)
},
greet: function(){
alert('Hello, my name is ' + this.get_name())
}
}
sub1 = {
get_name: function(){ return 'new month' }
}
sub2 = {
get_name = function(){
return 'current month'
}
}
factory = function(option){
return parent.init(option);
}
or
parent = function(){
this.greet = function(){
alert('Hello, my name is ' + this.get_name())
};
}
children1 = function(){
var self = new parent();
self.get_name = function(){ return 'new month' };
return self;
}
children2 = function(){
var self = new parent();
self.get_name = function(){ return 'current month' };
return self;
}
factory = function(option){
if (option == 1)
return new children1();
else
return new children2();
};