首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

请问小弟我对《javascript语言精粹》一书中“给类型增加方法”的困惑

2012-03-21 
请教我对《javascript语言精粹》一书中“给类型增加方法”的困惑?以下代码摘自《javascript语言精粹》4.7节(P32)

请教我对《javascript语言精粹》一书中“给类型增加方法”的困惑?
以下代码摘自《javascript语言精粹》4.7节(P32),为了方便测试我略微修改了下代码:

HTML code
<!DOCTYPE HTML><html lang="en-US"><script type="text/javascript">window.onload = function () {Function.prototype.method = function (name,func) {    this.prototype[name] = func;    return this;};Number.method('integer', function () {    return Math[this < 0 ? 'ceil' : 'floor'](this);    });var simple = document.getElementById('simple');var outputInt = (-10/3).integer();simple.innerHTML = outputInt;}</script><head>    <meta charset="UTF-8">    <title></title></head><body>一些无关紧要的内容<div id="simple">这里应该是一个测试数字</div></body></html>

我的困惑:
Number.method()这个方法是在什么地方定义的呢?或者说可不可以这么理解:Number下没有method()方法,于是Number.method()实际上查找的是Number.prototype.method(),如果是第二个假设的话,那么Number.prototype.method()又是在什么地方定义的呢?
第一个函数定义的可是Function.prototype.method()的呀?我把第一个函数换成Object.prototype.method()也是好使的,但是换成其他的关键字就不好使了,例如String.prototype.method()就不好使了,这是为什么呢?难道说这里的Object包含Function?如果真的是包括的话,那它和typeof的object值不包含 function值,如何区分?

[解决办法]
首先prototype是这样用的
http://www.blogjava.net/sterning/archive/2009/04/26/267583.html


然后是Number和Function的关系
http://bbs.51cto.com/thread-568228-1.html


然后改成Object也行,还是要参考第一个连接,那是Function和Object的关系了。

热点排行
Bad Request.