![]() |
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > javascript > 正文 |
|
|||
验证JS中Object和Function的关系的代码 | |||
2010-6-27 22:28:03 文/网络收集 出处:电脑软硬件应用网 | |||
本文以三段代码来论证一下javascript内置Object和Function的关系。 1、Function就是Object,Object就是Function
如你所看到的那样,通过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扩展的原型方法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 上面代码中最后有个问号的那一行是弹出true还是false呢?卖个关子,根据楼猪通篇直白而单纯的表述,你应该已经知道结果了,这里不公布答案了。 |
|||
关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 | |
Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号 |