sunqiang.leo
sunqiang.leo

Reputation: 235

Javascript: function.prototype.method

I guess most of you have seen the following code snippet:

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

I also know that it will affect all functions since they are all objects created by Function so that they can access method named "method", however I am confused why Function itself also can also access "method" like following:

Function.method('test', function () {return 1;});

Upvotes: 6

Views: 4266

Answers (4)

metadings
metadings

Reputation: 3848

Consider the following constructor function object:

var Construct = function () { };

And a prototype shared function:

Construct.prototype.hello = function (name) { console.log("Hello " + name); };

Now if you create a new object from the constructor, this gets the shared member function:

var c = new Construct();
c.hello("World");

The same as c is instanceof Construct Object, also any

  • function is instanceof Function and instanceof Object,
  • Function itself is instanceof Function and Object,
  • Construct is instanceof Function Object and also
  • Object is instanceof Function Object.

Every function statement and operator is a literal for a native new Function.
Every { } literal is a native new Object.

Objects created new by a constructor get the members of the constructor.prototype.
Objects can have any member for themselves, only members of prototypes get shared.

Upvotes: 1

Chris
Chris

Reputation: 7288

Edorka's answer is correct: Function is its own constructor (i.e. "parent").

Function.constructor;  // function Function() { [native code] }

Normally you can't do what you're doing. For example, this won't work:

f = function () {};
f.prototype.a = 5;
f.a;  // undefined

This kind of thing only works if you use a function as a constructor, like so:

f = function () {};
f.prototype.a = 5;
g = new f();
g.a;  // 5

But Function is weird, it is the constructor for all functions and is also a function itself, so it templates its properties off its own prototype. Hence you can call Function.method() in your code.

Upvotes: 8

Zirak
Zirak

Reputation: 39808

Because Function is itself a function:

typeof Function === 'function'
Object.getPrototypeOf(Function) === Function.prototype

And you can see it being called as a function (a form of indirect eval):

Function('return 1+2')() === 3

All that as defined in the spec.

zerkms asked in a comment above:

Which came first - the Function object or the Function prototype?

We have to understand that what's exposed to us, the puny programmers, is different than what's represented internally. This can be exemplified by overriding the Array constructor (tip: don't try this while writing an answer, you'll get a lot of errors):

new Array(0, 1, 2); //gives you [0, 1, 2]
Array = function () { return [4] };
new Array(0, 1, 2); //gives you [4]
//however,
[0, 1, 2] //will always give you [0, 1, 2]

This is because of a section in the spec (a bit down, in the "semantics" section):

Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.

Using the array literal (or array initializer as the spec calls it) you ensure that you use the built-in Array constructor.

Why did I give this example? First of all, because it's a fun example. Second, to demonstrate how what we do and what's actually done are different. To answer zerkms, the Function object most likely came first, but that was not the first function. We don't have access to that built-in function.

Upvotes: 5

Edorka
Edorka

Reputation: 1811

because new functions are using the Function's prototype, Functions method is using his own prototype methods too.

If you modify one of this methods or attributes and it belonged to a "parent" prototype all the other objects using this prototype will be affected.

Some literacy related to this strange subject: http://www.packtpub.com/article/using-prototype-property-in-javascript

Upvotes: 1

Related Questions