1、Function就是Object,Object就是Function
alert(Function
instanceof
Object);
//
true
alert(Object instanceof Function); // true
alert(Object instanceof Function); // true
如你所看到的那样,通过instanceof操作符,函数就是对象,对象就是函数。
2、既然1是成立的,那么Function扩展的原型方法,Object能“得到”吗?
alert(Object.funcMethod);
//
undefined
Function.prototype.funcMethod = function () {
/* some function method code here */
}
alert(Function.funcMethod);
alert(Object.funcMethod);
alert(Function.funcMethod === Object.funcMethod); // true
Function.prototype.funcMethod = function () {
/* some function method code here */
}
alert(Function.funcMethod);
alert(Object.funcMethod);
alert(Function.funcMethod === Object.funcMethod); // true
你没有看错,我们为Function扩展的原型方法funcMethod,Object实现了神奇的“不劳而获”。
3、既然1和2都成立,那么Object扩展的原型方法,Function能“得到”吗?!


alert(Function.objMethod);
//
undefined
Object.prototype.objMethod = function () {
/* some object method code here */
}
alert(Object.objMethod);
alert(Function.objMethod);
alert(Function.objMethod === Object.objMethod); // true or false?
Object.prototype.objMethod = function () {
/* some object method code here */
}
alert(Object.objMethod);
alert(Function.objMethod);
alert(Function.objMethod === Object.objMethod); // true or false?
上面代码中最后有个问号的那一行是弹出true还是false呢?卖个关子,根据楼猪通篇直白而单纯的表述,你应该已经知道结果了,这里不公布答案了。
最后,容楼猪在这里得意地自恋一下:个人认为上面这三段代码应该比原书中验证“函数就是对象的本质”的代码更具有说服力。