Feb 24, 2008

object's member

Sometime, a object's member comes from it's own definition, if it is not defined, will ask check its prototype's definition, same thing apply to the prototype, if prototype object has that definition, that is returned, if not prototype object'check its prototype object, the process continue until prototype object is undefined.

            function User()
            this.sayHi = function(){ alert("hi");};


            User.prototype.sayYes = function()

            User.sayHello = function()

            var u = new User();
            u.sayYes(); //this function comes from User.prototype

            alert(User.prototype.sayHi); //undefined
            u.sayHi();  //this function comes from the object u, in the constructor, sayHi is added to object

            User.sayHello(); //hello
            u.sayHello(); //throw exception

Please note the User itself is object created by Function, because alert(User.constructor == Function); is true. So its definition User.sayHello is not defined its prototype, u objects can not use this method.