我正在尝试遵循 Douglas Crockford 的“Javascript:优秀部分”。在第四章中,他谈到了增强类型,我觉得这令人兴奋。但是,我无法让他的示例代码工作。以下是他如何向 Number 实例添加整数方法:
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
有人可以帮我解释一下这里发生了什么吗?它们都是数字类型...
谢谢。
最佳答案
ceiling
应重命名为 ceil
。书中可能有错误?
关于javascript - Javascript 中的基本增强类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229995/