2013-01-09 36 views
2

我正在嘗試遵循Douglas Crockford的「Javascript:The Good Parts」。在第四章中,他談到了增強型,我覺得這很令人興奮。但是,我不能讓他的示例代碼工作。這裏是他如何給Number實例添加一個整數方法:Javascript中的基本增強類型

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

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

到目前爲止好。但在這裏,他是如何使用的增強方法整數,這是行不通的(至少不是在的jsfiddle):

document.writeln((-10/3).integer()); // -3 

但這個工程:

document.writeln((-3.3).integer()); // -3 

有人能爲我解釋這裏發生了什麼事?他們都是型號...

謝謝。

回答

3

ceiling應改名爲ceil。書中的錯誤可能是?