Constructors that produce frozen instances?

Axel Rauschmayer axel at rauschma.de
Sat Jun 8 20:22:19 PDT 2013


It’s difficult to do that if you want your constructors to remain subtypable. Is the following the best we can do? A constructor can only freeze if it is the first constructor that is invoked.

    function Point(x, y) {
        this.x = x;
        this.y = y;
        if (this.constructor === Point) {
            Object.freeze(this);
        }
    }
    function ColorPoint(x, y, color) {
        Point.call(this, x, y);
        this.color = color;
        if (this.constructor === ColorPoint) {
            Object.freeze(this);
        }
    }
    ColorPoint.prototype = Object.create(Point);
    ColorPoint.prototype.constructor = ColorPoint;

(More detailed write-up: http://www.2ality.com/2013/06/freezing-instances.html )

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

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


More information about the es-discuss mailing list