Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Javascript object prototype [duplicate]

Tags:

As I understand, in JavaScript every object has a prototype and it exposes some default properties. I have the following code where I'm trying to set the Year property of two objects through prototype. But both the calls are failing.

How can I override toLocalString() for any object if I don't have access to the prototype? Please note that the following code is to test on the prototype property, but my intention is to override the toLocalString() method.

var car = {     Make: 'Nissan',     Model: 'Altima' };  car.Year = 2014; alert(car.Year);  alert(car.prototype); // returns undefined  car.prototype.Year = 2014; // Javascript error  // --------------  function Car() {     this.Make = 'NISSAN';     this.Model = 'Atlanta'; }  var v = new Car(); v.prototype.Year = 2014; // JavaScript error alert(v.prototype); 
like image 699
wonderful world Avatar asked Oct 01 '14 19:10

wonderful world


People also ask

What is difference between __ proto __ and prototype?

prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.

What is __ proto __ in JavaScript?

The __proto__ property is a default property added to every object. This property points to the prototype of the object. The default prototype of every object is Object. prototype . Therefore, the __proto__ property of the person object points to the Object.

How do you get the prototype of an object?

getPrototypeOf() The Object. getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.


2 Answers

You do have access to the prototype property, but it is only present on Functions.

var car = {     Make: 'Nissan',     Model: 'Altima' };  

This is the same as:

var car = new Object(); car.Make = 'Nissan'; car.Model = 'Altima';  

So, car.__proto__ === Object.prototype.

And car.prototype === undefined as the prototype property is only present on Functions (as I already said).

function Car() {     this.Make = 'NISSAN';     this.Model = 'Atlanta'; } 

Here Car.prototype points to an instance of Object because Car is a function and when function declarations are evaluated their prototype is set to an instance of Object.

Car.prototype.Year = 2014; //all Car *instances* will have a Year property set to 2014 on their prototype chain.  var c = new Car(); //create an instance console.log(c.Year); //2014 

Overriding a method present on the prototype chain for an object is as simple as creating a corresponding method on the object:

var myObject = new Object(); myObject.toLocaleString = function() {   //my own implementation }; 
like image 79
Ben Aston Avatar answered Oct 29 '22 03:10

Ben Aston


You probably want to modify the constructor function prototype:

function Car(year, make, model) {   this.year  = year;   this.make  = make;   this.model = model; }  Car.prototype.toLocaleString = function() {   return [this.year, this.make, this.model].join(' '); };  var civic = new Car(2014, 'Honda', 'Civic'); civic.toLocaleString(); // => "2014 Honda Civic" 

This MDN article on Object.prototype might be helpful.

like image 30
maerics Avatar answered Oct 29 '22 05:10

maerics