以下代码摘自《javascript语言精粹》4.7节(P32),为了方便测试我略微修改了下代码:<!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值,如何区分?