Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding prototype Property of function

Taken an example if

var Func = function(){}

Here Func has property called prototype and I can add my custom methods like the following.

Func.prototype.move = function(){ //do something }

As per my understanding here prototype is just another property of Func which is by default provided by the interpreter and is not used to delegate any functionality, ie. there is nothing like

Func.move()

Applying the same logic I am creating another property of the same function like the following

Func.method = function(){ //do something }

Now if a create a new object

var obj = new Func();

Here there is obj.move() but obj.method() wont be there. If prototype is just another property with no magical advantages then why this certain behavior ?? Thanks in advance !

like image 707
Shouvik Avatar asked Jul 07 '15 19:07

Shouvik


People also ask

What is the prototype of the function?

A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).

What is a prototype property?

Whenever we create a JavaScript function, JavaScript adds a prototype property to that function. A prototype is an object, where it can add new variables and methods to the existing object. i.e., Prototype is a base class for all the objects, and it helps us to achieve the inheritance.

How do I see the prototype of a function?

We can access the function's prototype property using functionName. prototype . As seen from the above image prototype property of the function is an object (prototype object) with two properties: constructor property which points to Human function itself.

Do all functions have a prototype property?

Note: Not all Function objects have the prototype property — see description.


4 Answers

When you use new the prototype property of the function will be used as a template for the internal [[Prototype]] property of the instance object. This is exposed in some browsers as __proto__, not to confuse with prototype which again, it is just a regular property.

When you attach a property to the function directly, as opposed to the prototype, you are basically using the function as a namespace. Since functions are objects, they can have arbitrary properties, and others are built-in, like prototype and name for example. In effect you are creating something similar to a static method, one that doesn't depend on the instance, that doesn't use this.

like image 171
elclanrs Avatar answered Nov 01 '22 02:11

elclanrs


The prototype property of a function does have a special purpose: it will be set as the internal prototype of objects created by using this function as a constructor function (ie. when you use new).

So when you write var obj = new Func(); obj doesn't have an own property move, but when the lookup fails, it gets delegated upwards the prototype chain: to the prototype of obj, which is the Func.prototype you set earlier, which does have a move method.

On the other hand, adding move to Func is just creating a property of the function itself (functions are objects too, so you can add properties to them) - you can call it with Func.move(), but it will have basically nothing to do with obj.

If you want to read up more about how a function's prototype property works, I recommend Eloquent Javascript's chapter The Secret Life of Objects.

like image 22
doldt Avatar answered Nov 01 '22 01:11

doldt


With move, you are adding the function to the object's prototype, thus it is available to newly created objects. With method, you are adding the function to the already created object Func.

like image 21
WhiteHat Avatar answered Nov 01 '22 01:11

WhiteHat


prototype property is used to lookup the property/function in prototype chain. So when you ask a property of any object first its looked up on that object itself, if not found then its been searched through the object's prototypical chain upto Object. So particular to your example, you are adding method property directly on the Func object so it would be available only on that instance in this case on Func & you are adding the move property to the prototype of Func, so it would be made available through the prototypical chain lookup.

like image 1
vinayakj Avatar answered Nov 01 '22 02:11

vinayakj