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;});
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;});
Share
Improve this question
asked Jul 3, 2013 at 11:07
sunqiang.leosunqiang.leo
2353 silver badges8 bronze badges
4 Answers
Reset to default 8Edorka'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.
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 ment 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()
whereArray
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.
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./article/using-prototype-property-in-javascript
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
isinstanceof Function
andinstanceof Object
,Function
itself isinstanceof Function
andObject
,Construct
isinstanceof Function Object
and alsoObject
isinstanceof 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.