Nov 27, 2008

new this

The Animal.init is interesting. "this" refer to Animal function, which can be newed. It shows that function is alsow object, some object can be newed. some can not be newed

    function Animal() { };
    Animal.prototype = {
        name: "Animal",
        sayName: function() {
            alert(this.name);
        }
    }

    Animal.init = function() {
//"this" refer to Animal function, 
//which can be newed. It shows that function 
//is alsow object, some object can be newed. some can not be newed
        return new this; 
    }

    var an = Animal.init();
    an.sayName();