6

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

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

"zerkms asked" --- it was actually a joke referred to a "chicken vs egg" problem :-)
@zerkms Of course, and in this case, the chicken was actually brought to us by a more sentient being. Problem forever solved now?
1

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

3 Comments

Which came first - the Function object or the Function prototype?
the prototype, if the object itself can't get de method or attribute if asks it to its prototype , or prototype's prototype....
I just think for Function itself, it should be the object of Object, thus it will only look for the prototype of Object instead of Function itself.
1

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.

2 Comments

I just think for Function itself, it should be the object of Object, thus it will only look for the prototype of Object instead of Function itself.
@user2546240 it is. don't confuse with typeof's results. just try instanceof. because typeof is a string it can't say function and object the same time. instanceof is more accurate because it knows that function is both object of Function and object of Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.