Javascript: function.prototype.method

I assume most of you saw the following code snippet:

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

I also know that this will affect all functions, since they are all objects created by the function, so that they can access the method called "method", however I am confused why the function itself can also access the "method", for example to the following:

 Function.method('test', function () {return 1;}); 
+4
source share
4 answers

Edorka's answer is correct: a function is its own constructor (ie, "parent").

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

Usually you cannot do what you do. For example, this will not work:

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

This thing only works if you use the function as a constructor, for example:

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

But the function is strange, it is a constructor for all functions and is also a function itself, so it builds its properties from its own prototype . Therefore, you can call Function.method() in your code.

+6
source

Because Function itself is a function:

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

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

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

Everything is defined in the specification .

zerkms asked in the comment above :

What happened before - a Function object or a prototype of a function?

We must understand that what is exposed to us, small programmers, is different from what is represented domestically. This can be illustrated by overriding the Array constructor (hint: do not try this when writing an answer, you will get many 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 due to a section in the specification (a bit in the semantics section):

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

Using an array literal (or an array initializer when invoking the specification), you guarantee that you are using the built-in Array constructor.

Why did I give this example? First of all, because this is a fun example. Secondly, to demonstrate what we are doing and what is actually done, different. Most likely, a Function object appeared to answer zerkms, but this was not the first function. We do not have access to this built-in function.

+5
source

since new functions use the Function prototype, the Function method also uses its own prototype methods.

If you change one of these methods or attributes and belong to the "parent" prototype, all other objects using this prototype will be affected.

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

+1
source

Consider the following constructor function object:

 var Construct = function () { }; 

And the prototype of the joint function:

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

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

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

Same as c - instanceof Construct Object , also any

  • function - instanceof Function and instanceof Object ,
  • function - instanceof Function and Object ,
  • Construct - instanceof Function Object , as well as
  • Object instanceof Function Object .

Each function statement and operator are literals for the native new Function .
Each literal { } is a native new Object .

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

+1
source

Source: https://habr.com/ru/post/1489461/


All Articles