Apr 15, 2009

Object vs Function

Object.prototype.sayhi = function() { alert("hi"); }

var o = {};
alert(o.constructor); //function Object(){[native code]}, created by function Object()
alert(Object.prototype); //[object Object], ultimate prototype
o.sayhi(); //show hi

Function.prototype.saybye = function() { alert("bye"); }

alert(f.constructor); //function Function(){[native code]}, created by function Fuction()
alert(Function.prototype); //function prototype{[native code]},
//I think Function.prototype still link to an object constant {},
//so that it can be route to  [object Object], the ultimate prototype

alert(Function.prototype.constructor); //function Function(){[native code]}, created by function Fuction()
alert(f.prototype); //[object Object], ultimate prototype

function f() { }
f.sayhi(); //show hi
f.saybye(); //show bye

    alert(document.constructor); //[objectHTMLDocument
    alert(document.constructor.prototype); //[Interface prototype object]
    alert(document.constructor.prototype.constructor); //null in IE, function Object() in firefox

No comments:

Post a Comment