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);