WATCH AND LEARN

I learn by doing and seeing

JavaScript Gotchas

[ ] [ Tags JavaScript, geek ]

How to use JavaScript declare class

  1. 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;
    };
}
  • Instantiate an object
1
2
var apple = new Apple('macintosh);
apple.color = 'reddish';
  • Declare instance method
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;
    }
};