Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add value using prototype in function instance [duplicate]

Tags:

javascript

I have one constructor function in my code. I have create instance of that constructor. In newly created instance I want to add value or function by using prototype method. But I getting error while doing this. Here is my code fiddle

function a(){
this.d=9
}
a.prototype.one=1;
a.prototype.two=2;


var j= new a();
j.prototype.three=3;

console.log(j)
like image 379
Jitender Avatar asked Jan 08 '15 09:01

Jitender


People also ask

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

What is __ proto __ in JavaScript?

__proto__ is a way to inherit properties from an object in JavaScript. __proto__ a property of Object. prototype is an accessor property that exposes the [[Prototype]] of the object through which it is accessed. POSTly is a web-based API tool that allows for fast testing of your APIs (REST, GraphQL).

How do you set the prototype of one object to another?

The Object. setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. All JavaScript objects inherit properties and methods from a prototype. It is generally considered the proper way to set the prototype of an object.

How does prototypal inheritance work?

The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object. getPrototypeOf and Object.


2 Answers

It should be a prototype of the constructor function, not the object this function produces:

a.prototype.three = 3;

You can't access object's prototype with the prototype key, because prototype reference is not exposed like this. You could do it using __proto__ property though, but this is deprecated. If you need to get a prototype of the object you can make use of Object.getPrototypeOf method:

Object.getPrototypeOf(j) === a.prototype; // true

It's a little confusing here because the word "prototype" sort of means two things. Function prototype is an object that is used when new object is constructed when the function is used like a constructor. Object prototype is a reference to the object which stores inherited methods.

like image 93
dfsq Avatar answered Oct 05 '22 22:10

dfsq


J's prototype is undefined, because you cant access it directly, so you cant directly set the property three to the prototype of j.

This is why you are able to add properties to a's prorotype but not to j's prototype, you can try

j.three=3; 

Or a.prototype.three = 3;

fiddle http://jsfiddle.net/s4g2n453/4/

like image 22
Naeem Shaikh Avatar answered Oct 06 '22 00:10

Naeem Shaikh