You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note the "return" statement used in the current code vs. the proposed fix. A constructor function called by itself (without new) should never return a value. But even if users avoid return values from their own constructors, I can think of at least one bug this might cause, or at least one nice feature this would allow if the above code were changed to avoid a return value.
For example, to leverage inheritance in Backbone, currently I must extend Backbone.Model (or Collection, View, etc.). However, what if I just wanted to define a minimal set of classes that extend Object. I might try this:
// let anything to be extended
Object.extend = Backbone.Model.extend;
// namespace
var Acme = {};
// define classes
Acme.Person = Object.extend();
Acme.Warrior = Acme.Person.extend();
Acme.Ninja = Acme.Warrior.extend();
// instantiate
var mary = new Acme.Person();
var will = new Acme.Warrior();
var john = new Acme.Ninja();
// unit tests
ok(mary instanceof Object && mary instanceof Acme.Person && mary.constructor === Acme.Person);
ok(will instanceof Object && will instanceof Acme.Person && will instanceof Acme.Warrior && will.constructor === Acme.Warrior);
ok(john instanceof Object && john instanceof Acme.Person && john instanceof Acme.Warrior && john instanceof Acme.Ninja && john.constructor === Acme.Ninja);
The unit tests fail when the return statement is used. However, if you remove the return from your parent constructor call, then all tests will pass.
In other words, if this change is made, then we can also use Backbone to define simple class inheritance without using Backbone-specific classes.
The text was updated successfully, but these errors were encountered:
From the "inherits" function:
Proposed fix:
Note the "return" statement used in the current code vs. the proposed fix. A constructor function called by itself (without new) should never return a value. But even if users avoid return values from their own constructors, I can think of at least one bug this might cause, or at least one nice feature this would allow if the above code were changed to avoid a return value.
For example, to leverage inheritance in Backbone, currently I must extend Backbone.Model (or Collection, View, etc.). However, what if I just wanted to define a minimal set of classes that extend Object. I might try this:
The unit tests fail when the return statement is used. However, if you remove the return from your parent constructor call, then all tests will pass.
In other words, if this change is made, then we can also use Backbone to define simple class inheritance without using Backbone-specific classes.
The text was updated successfully, but these errors were encountered: