User1183902823 posted
see 3 different approach to create js class and object.
Approach 1
--------------
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo1 = function() {
return this.color + ' ' + this.type + ' apple';
};
}
Apple.prototype.getInfo2 = function() {
return this.color + ' ' + this.type + ' apple';
};
var apple = new Apple('macintosh');
apple.color = "reddish";
alert(apple.getInfo1());
alert(apple.getInfo2());
Approach 2
--------------
var apple = {
type: "macintosh",
color: "red",
getInfo: function () {
return this.color + ' ' + this.type + ' apple';
}
}
// calling this way
apple.color = "reddish";
alert(apple.getInfo());
Approach 3
--------------
var apple = new function() {
this.type = "macintosh";
this.color = "red";
this.getInfo = function () {
return this.color + ' ' + this.type + ' apple';
};
}
// calling this way
apple.color = "reddish";
alert(apple.getInfo());
why different approach take to declare a function with apple class
getInfo: function () {
return this.color + ' ' + this.type + ' apple';
}
this.getInfo = function () {
return this.color + ' ' + this.type + ' apple';
};
some time they : function and some time they = then function name....why?
how could a declare private function inside apple class?
code taken from https://www.phpied.com/3-ways-to-define-a-javascript-class/
guide me.