Question regarding ES5

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Feb 16 17:21:11 PST 2011


The error looks correct to me. By freezing Type.proto you make all its own properties "read only".  One of those is the constructor that is automatically created on every func.prototype object.  When you assign to object.constructor you are trying to over-ride an inherited read-onoy property.  ECMAScript has never allowed this.  You can say:
      Object.defineProperty(object,"constructor",{value: function Foo() {}, /* any other attributes you want to set */});
to over-ride the inherited constructor property.
Or you can restructure you code so you do the freeze after you do the assignment.

On Feb 16, 2011, at 5:09 PM, Irakli Gozalishvili wrote:

> Hi,
> 
> I've run into one issue and even after reading ES5 specs several times it's not clear to me what should be an expected behavior:
> 
> Currently on Firefox nightly following code:
> 
> (function() {
> "use strict";
> 
> function Type() {}
> Object.freeze(Type.prototype);
> var object = Object.create(Type.prototype);
> object.constructor = function Foo() {};
> 
> return object
> })();
> 
> throws TypeError: object.constructor is read-only
> while on chrome 
> 
> it returns object with constructor Foo
> 
> I would like to know what is an expected behavior to fill a bug to an appropriate project.
> 
> 
> Thanks!
> --
> Irakli Gozalishvili
> Web: http://www.jeditoolkit.com/
> Address: 29 Rue Saint-Georges, 75009 Paris, France
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110216/68fc87a3/attachment-0001.html>


More information about the es-discuss mailing list