Search code examples
javascriptjavascript-objectsprototype-programming

javascript changing Object.prototype not work


It is just for experiment. The following code seems not able to change Object.prototype to null.

Object.prototype = null;
var o = new Object();

Is it just because it is 'Object' or built-in types? I think we can change it for user-defined types. Sorry if I'm wrong


Solution

  • From MSDN
    Some more info here: http://msdn.microsoft.com/en-us/library/f5s9ycex(v=vs.94).aspx

    All intrinsic JavaScript objects have a prototype property that is read-only. Properties and methods may be added to the prototype, but the object may not be assigned a different prototype. However, user-defined objects may be assigned a new prototype. The method and property lists for each intrinsic object in this language reference indicate which ones are part of the object's prototype, and which are not.

    From the specification
    Some more info here: http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.1

    15.2.3.1 Object.prototype

    The initial value of Object.prototype is the standard built-in Object prototype object (15.2.4).

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