Skip to content

Support ES6 method-shorthand constructors in extend (#4213)#4300

Open
BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli:fix-4213-es6-constructor
Open

Support ES6 method-shorthand constructors in extend (#4213)#4300
BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli:fix-4213-es6-constructor

Conversation

@BigBalli
Copy link
Copy Markdown

@BigBalli BigBalli commented Apr 6, 2026

Fixes #4213.

Problem

ES6 method shorthand (constructor() {}) produces a non-constructable
function with no prototype property. Passing such a constructor to
extend previously caused new Subclass() to throw
Subclass is not a constructor:

const MyModel = Backbone.Model.extend({
  constructor() {
    Backbone.Model.apply(this, arguments);
  }
});
new MyModel(); // TypeError: MyModel is not a constructor

This forces users on modern JS to write the awkward mixed form
(constructor: function() {} next to method shorthand) or to give up on
extend altogether.

Fix

Detect the shorthand case by checking for the absence of prototype on
the supplied constructor, and in that case wrap it in a thin function so
new continues to work. The traditional constructor: function() {}
form is unchanged and still uses the supplied function directly, so the
edge case noted in the issue (Model === proto.constructor) remains
true for ES5 users.

Test

Added a regression test in test/model.js that defines the
constructor via ES6 method shorthand (constructed via eval so the
test file still parses on engines without shorthand support) and
verifies that new Model({foo: 'bar'}) returns a usable instance.

npm run lint passes.

ES6 method shorthand (`constructor() {}`) produces a non-constructable
function with no `prototype` property. Passing such a constructor to
`extend` previously caused `new Subclass()` to throw
"Subclass is not a constructor".

Detect the shorthand case (absence of `prototype`) and wrap the
supplied constructor in a thin function so `new` continues to work.
The traditional `constructor: function() {}` form is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ES6 method definitions for constructors

1 participant