constructorExtends
Toggles the behavior of a constructor function called
without the new
keyword to extend the constructor function or
create a new instance.
var animal = Animal();
// vs
var animal = new Animal();
Boolean
If constructorExtends
is:
true
- the constructor extendsfalse
- a new instance of the constructor is created
This property defaults to false.
Example of constructExtends as true
:
var Animal = Construct.extend({
constructorExtends: true // the constructor extends
},{
sayHi: function() {
console.log("hai!");
}
});
var Pony = Animal({
gallop: function () {
console.log("Galloping!!");
}
}); // Pony is now a constructor function extended from Animal
var frank = new Animal(); // frank is a new instance of Animal
var gertrude = new Pony(); // gertrude is a new instance of Pony
gertrude.sayHi(); // "hai!" - sayHi is "inherited" from Animal
gertrude.gallop(); // "Galloping!!" - gallop is unique to instances of Pony
The default behavior is shown in the example below:
var Animal = Construct.extend({
constructorExtends: false // the constructor does NOT extend
},{
sayHi: function() {
console.log("hai!");
}
});
var pony = Animal(); // pony is a new instance of Animal
var frank = new Animal(); // frank is a new instance of Animal
pony.sayHi() // "hai!"
frank.sayHi() // "hai!"
By default to extend a constructor, you must use extend.