javascript中的原型继承

javascript中的原型继承,javascript,Javascript,我从Javascript Good Parts一书中选取了以下示例,并在firefox中运行: Function.prototype.method = function(name, value){ this.prototype[name] = value; return this; } Number.method('integer', function ( ) { return Math[this < 0 ? 'ceil' : 'floor'](this); }

我从Javascript Good Parts一书中选取了以下示例,并在firefox中运行:

Function.prototype.method = function(name, value){
    this.prototype[name] = value;
    return this;
}

Number.method('integer', function (  ) {
    return Math[this < 0 ? 'ceil' : 'floor'](this);
});

console.log((-10 / 3).integer());
Function.prototype.method=函数(名称、值){
this.prototype[name]=值;
归还这个;
}
Number.method('integer',function(){
返回数学[this<0?'ceil':'floor'](this);
});
console.log(-10/3.integer());
这是我对它工作原理的理解。我们使用()高优先级运算符来计算-10/3,它给出了这个64位浮点值-3.3。。。由于JavaScript没有浮点、十进制或整数类型,因此返回值为Number。因此,我们对Number对象调用integer()函数,该对象位于其原型链中。“this”必须指数字对象所持有的值。我们使用三元来检查它是负数还是正数,在本例中,我们运行:Math'ceil',因为它是负数。这将使数字向上舍入得到-3


这个脚本可以工作,但我不明白为什么Number.method不会抛出异常,因为Number不是函数的一种类型,方法是在函数的原型上定义的,而不是Number的原型,Number也不是从函数继承的,它只继承自Object.prototype。

请在控制台中尝试这些示例。

Number
是一个函数

typeof Number
"function"
证实了这一点

因此,
Number
确实继承自
函数

Number instanceOf Function
"true"
与所有其他JavaScript对象一样,
函数继承自
对象

Function instanceof Object
true

这是一个instanceof函数:
Number instanceof Function===true
@PatrickEvans那么为什么Number缺少执行能力呢?()那么为什么Number不能像函数一样执行呢?它可以像函数一样执行:
var num=Number(13)@PatrickEvans那么,当所有东西都从函数继承时,Object.prototype和Object本身的目的是什么?所有东西都不是从函数继承的,所有东西都是从Object继承的。例如:
“string”instanceof Function===false
@JohnMerlino并非所有内容都从函数继承!有很多JavaScript对象不是函数。