can-construct
Provides a way to easily use the power of prototypal inheritance without worrying about hooking up all the particulars yourself. Use can-construct.extend to create an inheritable constructor function of your own.
new Construct( ...args )
Creates a new instance using Construct's constructor functions.
Parameters
- args
{*}
:The arguments passed to the constructor.
Returns
{Object}
:
The instantiated object.
Use
In the example below, Animal
is a constructor function returned by can-construct.extend. All instances of Animal
will have a speak
method, and the Animal
constructor has a legs
property.
import Construct from "can-construct";
const Animal = Construct.extend( {
legs: 4
},
{
speak: function() {
console.log( this.sound );
}
} );
An optional setup function can be specified to handle the instantiation of the constructor function.
const Animal = Construct.extend( {
legs: 4,
setup: function( sound ) {
return [ sound ];
}
},
{
speak: function() {
console.log( this.sound );
}
} );
setup returns {Array|undefined} If an array is returned, the array's items are passed as arguments to init.
In addition init can be specified which is a method that gets called with each new instance.
const Animal = Construct.extend( {
legs: 4,
init: function( sound ) {
this.sound = sound;
}
},
{
speak: function() {
console.log( this.sound );
}
} );
For more information on deciding when to use setup or init see the bottom of the setup documentation.
You can make instances of your object by calling your constructor function with the new
keyword. When an object is created, the init
method gets called (if you supplied one):
const panther = new Animal( "growl" );
panther.speak(); // "growl"
panther instanceof Animal; // true
Plugins
There are plugins available to help make using can-construct
even simpler.
- can-construct-super allows you to easily call base methods by making
this._super
available in inherited methods.