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
有人能为我解释这里发生了什么事?他们都是型号...
谢谢。