我正在学习Javascript,所以这个问题对于大多数JS程序员来说似乎都很荒谬。我正在阅读Javascript:好的部分,我无法使这段代码工作:
Function.prototype.method = function(name,func){
this.prototype[name] = func;
return this;
}
Number.method('integer', function(){
return Math[ this <0? 'ceiling' : 'floor'](this);
});
document.writeln(Math.floor(3.4)+"");
document.writeln((-10/3).integer());
正如您可能猜到的那样,第一个document.writeln函数显示“3”应该是,但第二个没有显示任何内容,错误是:“TypeError:Math [”floor“]不是函数”althoug它确实是一种功能。
我很确定这是愚蠢的,但我不知道为什么它不起作用。谢谢你的时间。
法比安
答案 0 :(得分:3)
将'天花板'变为'ceil'并且运行良好,我测试了它:
Number.method('integer', function(){
return Math[ this <0? 'ceil' : 'floor'](this);
});
答案 1 :(得分:1)
“floor”确实是一个数学函数。但是你的代码返回了应该是“ceil”的“天花板”