Skip to content Skip to sidebar Skip to footer

Why Is Reassigning Object.prototype Not Working?

Why this is not working? // this one works as I expected, when objSayHello() Object.prototype.objSayHello = function(){alert('Hello,from OBJECT prototype')}; // NOT working ! Objec

Solution 1:

  1. Because you've replaced the Object prototype, so you're adding a objSayHello method to any object descending from Object (all objects).

  2. Don't replace Object.prototype.

What you probably want is:

someObj.prototype.objSayHello = function(){alert('Hello,from OBJECT prototype')};

Then to call it with:

someObj.objSayHello();

What you seem to be meaning to do is:

Object.prototype.objSayHello = function(){alert('Hello,from OBJECT prototype')};

But that is probably a bad idea as it will conflict with iterators (for...in) if not handled correctly.

Solution 2:

Object.prototype is for some reason a const, which means read only.

Solution 3:

Object.prototype.objSayHello = function(to){alert('Hello, ' + to)};

The above statement means that you attached the objSayHello function to all the instances that will be created as every instance is child of Object so the attached event bind to all. For instance,

var newObject = {};
var anotherNewObject = newObject();

functionperson(name) {
 this.name = name;
}
var newPerson = newPerson("Ramiz");

newObject.objSayHello("newObject");
anotherNewObject.objSayHello("anotherNewObject");
newPerson.objSayHello(this.name);

While, the other statement is incorrect and will be ignored completely as you're about to discard the prototype of an object which is parent of all. If the prototype of Object can be override then all native instances functionality will be gone. To avoid such a mistake I think this is ignore to be override.

Solution 4:

Eyelidlessness is wrong, you simply can't reassign the prototypes of intrinsic types like Object, Number, etc. You can only append new properties.

> Number.prototype
  -> Number
> Number.prototype = {}  // Reassignment fails
  -> Object
> Number.prototype
  -> Number
> Number.prototype.objSayHello = 'hi'// Appending succeeds
  -> 'hi'
> n = newNumber(); n.objSayHello
  -> 'hi'

If you are using your own objects, then you can update or reassign the prototype (note: reassigning the prototype will only affect new objects, not existing ones).

Do not change intrisic prototypes! This is a 'bad thing' because it can cause hard-to-find side effects. If you need a basic type with new properties, then create a new object with the prototype pointing to the basic object:

NewObject = function() {};NewObject.prototype = Object.prototype;NewObject.prototype.value = 123;

Now you have a constructor that creates new objects that inherit properties from both its own prototype (eg 'value') as well as the Object prototype ('toString', 'hasOwnProperty'...).

Solution 5:

The ECMAScript language specification has this for Object.prototype:

The initial value of Object.prototype is %Object.prototype%.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

You'll find the same "[[Writable]]: false" specification for the prototype properties of all other native JavaScript constructors, such as Function, Number, RegExp, Date, ...etc.

Post a Comment for "Why Is Reassigning Object.prototype Not Working?"