Proposal: Improve syntax for inline anonymous class instantiations

Igor Vaynberg igor.vaynberg at gmail.com
Fri Jan 6 22:11:57 UTC 2017


Given a simple class with an abstract method "populateItem"

class ArrayView extends Container {
    constructor(id, model) {
        super(id);
        this.model = model;
    }

    // methods referencing "populateItem" omitted for clarity
}

the current anonymous instantiation syntax looks like this:

this.add(new class extends ArrayView {
    populateItem(item) {
        item.add(new Checkbox("check", new PropertyModel(item.model, "done")));
        item.add(new Label("title", new PropertyModel(item.model, "title")));
    }
}
("items", itemsModel)
);

The problem with this syntax is that it pushes the constructor
parameters below the class body which I think causes two problems:

When scanning code constructors often contain the piece of information
that helps locate the anonymous class, which currently requires the
developer to look back. This is especially problematic for anonymous
classes with long class bodies.

When writing code I usually think about the constructor first, so it
seems it would be preferable to write it before moving onto working on
the class body. This is also the reason why constructors are usually
placed toward the top of named classes' source.

A better syntax would move the constructor parameters between the
super class name and the class body:

this.add(new class extends ArrayView("items", itemsModel) {
    populateItem(item) {
        item.add(new Checkbox("check", new PropertyModel(item.model, "done")));
        item.add(new Label("title", new PropertyModel(item.model, "title")));
    }
});

If possible it would also be great to get rid of the "class extends"
keywords for this usecase:

this.add(new ArrayView("items", itemsModel) {
    populateItem(item) {
        item.add(new Checkbox("check", new PropertyModel(item.model, "done")));
        item.add(new Label("title", new PropertyModel(item.model, "title")));
    }
});

Thoughts?


Thanks,
-igor


More information about the es-discuss mailing list