Lim H.
Lim H.

Reputation: 10050

Basic Augmenting Types in Javascript

I'm trying to follow Douglas Crockford's "Javascript: The Good Parts". In chapter 4, he talks about Augmenting Type, which I find mind-blowing. However, I can't get his example codes to work. Here is how he adds an integer method to Number instances:

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

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

So far so good. But here is how he uses the augmented method integer and it doesn't work (at least not in jsFiddle):

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

But this works:

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

Could someone explain for me what happened here? They are both of type number...

Thanks.

Upvotes: 2

Views: 238

Answers (1)

Wireblue
Wireblue

Reputation: 1509

ceiling should be renamed to ceil. An error in the book maybe?

Upvotes: 3

Related Questions