user1376727
user1376727

Reputation: 43

Is everything linked to Function prototype as well?

I am reading JavaScript The Good Parts without prior js knowledge and this bit baffled me. I think I need clarification.

JavaScript allows the basic types of the language to be augmented. In Chapter 3, we saw that adding a method to Object.prototype makes that method available to all objects. This also works for functions, arrays, strings, numbers, regular expressions, and booleans. For example, by augmenting Function.prototype, we can make a method available to all functions:

Then goes on with this example:

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

Now every basic type has a "method" method so new functions can be defined for them, like:

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

But the book previously noted everything links to Object not to Function! How is this working out?

Upvotes: 4

Views: 59

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318568

No, only functions have Function.prototype. Number is a constructor function so it is "linked" to that prototype.

Here's what the nodejs/V8 shell says about Number:

> Number
[Function: Number]
> typeof Number
'function'

Upvotes: 3

Related Questions