Search code examples
javascriptextended-procedures

TypeError: Math["floor"] is not a function


I'm learning Javascript, so this question may seem ludicrous for most JS coders. I'm reading Javascript: The good parts, and I can't make this piece of code work:

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());

As you probably guessed it, the first document.writeln function displays "3" as it should be, but the second one displays nothing and the error is: "TypeError: Math["floor"] is not a function" althoug it is indeed a function.

I'm pretty sure this is stupid, but I don't find why it doesn't work. Thanks for your time.

Fabien


Solution

  • turn 'ceiling' to 'ceil' and it run well, I tested it:

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