How to use JavaScript declare class
- Using a function
1
2
3
4
5
6
7
| function Apple(type) {
this.type = type;
this.color = "red";
this.getInfo = function () {
return this.color;
};
}
|
1
2
| var apple = new Apple('macintosh);
apple.color = 'reddish';
|
1
2
3
| Apple.prototype.getInfo = function () {
return this.color;
};
|
- Singleton - using object literal
1
2
3
4
5
6
7
| var apple = {
type: 'hey',
color: 'blue',
getInfo: function () {
return this.color;
}
};
|