电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > javascript > 正文
验证JS中Object和Function的关系的代码
验证JS中Object和Function的关系的代码
2010-6-27 22:28:03  文/网络收集   出处:电脑软硬件应用网   

本文以三段代码来论证一下javascript内置Object和Function的关系。

1、Function就是Object,Object就是Function


alert(Function instanceof Object); // 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扩展的原型方法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?

上面代码中最后有个问号的那一行是弹出true还是false呢?卖个关子,根据楼猪通篇直白而单纯的表述,你应该已经知道结果了,这里不公布答案了。

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    js或jquery获取网页控件的offset值的…
    不支持ASP的空间能用JS吗?
    关于20个Ajax关键问题的疑问及解答
    extjs重新设置按钮button的text的方…
    58项经典的JS代码
    使用JS自动切换背景音乐的方法
    一些常用正则表达式语法参考(数字、…
    JavaScript框架的实用函数
    推荐一些实用JS测试和验证工具给大家
    示例jquery使用ajax调用Webservice返…
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号