http://brendaneich.com/2011/10/jsconf-eu/

I’d be sad not to have class literals in JavaScript. However, with the new 
object literal extensions, things are still much better than in ES5. Compare: 
The ES5 version is clumsy.

    // Super
    function Person(name) {
        this.name = name;
    }
    Person.prototype.describe = function() {
        return "Person called "+this.name;
    };

    // Sub
    function Employee(name, title) {
        Person.call(this, name);
        this.title = title;
    }
    Employee.prototype = Object.create(Person.prototype);
    Employee.prototype.constructor = Employee;
    Employee.prototype.describe = function() {
        return Person.prototype.describe.call(this)+" ("+this.title+")";
    };

The ES.next version is quite nice:

    // Super
    function Person(name) {
        this.name = name;
    }
    Person.prototype .= {
        describe() {
            return "Person called "+this.name;
        }
    }
    
    // Sub
    Person <| function Employee(name, title) {
        super(name);
        this.title = title;
    }
    Employee.prototype .= {
        describe() {
            return super.describe(this)+" ("+this.title+")";
        }
    };

This is not bad at all, quite teachable! Having a single construct instead of 
piecing things together via multiple assignments might be a problem in some 
cases. Then you could use the latest incarnation of Allen’s class pattern:

    Person <| function Employee(name, title) {
        super(name);
        this.title = title;
    }.={
        prototype.={
            describe() {
                return super.describe(this)+" ("+this.title+")";
            }
        }
        // “class” variables go here
    }

[I’m using the destructive version of the extension operator, as shown in 
Brendan’s blog post]

-- 
Dr. Axel Rauschmayer
a...@rauschma.de

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



_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to