I am a noob to javascript.
I wanted to know why do we have to use this while defining properties and functions in the javascript. for example
function Apple (type) {
this.type = type;
this.color = "red";
this.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
}
var apple = new Apple('macintosh');
apple.color = "reddish";
alert(apple.getInfo());
I know this refers to the object that calls the class. We used this in c++ and java. we defines a class something like this
class apple {
char type ;
char color ;
returntype getInfo (){
this.color = 'red';
this.type = 'something'
}
I thought its weird to have this in javascript for declaring properties also. Any reason behind this ?
thisin Java, and JavaScript doesn't explicitly have a "constructor" function separate from definitions. What's your real question?