constructorExtends

  • property

{Boolean}

 

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 extends
  • false - a new instance of the constructor is created

This property defaults to false.

Example of constructExtends is true:

var Animal = can.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 = can.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.