• function

init can.Construct.prototype.init


Called when a new instance of a can.Construct is created.



  1. args {*}

    the arguments passed to the constructor (or the elements of the array returned from can.Construct.prototype.setup)

If a prototype init method is provided, it is called when a new Construct is created, after can.Construct.prototype.setup. The init method is where the bulk of your initialization code should go, and a common thing to do in init is to save the arguments passed into the constructor.


First, we'll make a Person constructor that has a first and last name:

can.Construct("Person", {
    init: function(first, last) {
        this.first = first;
        this.last  = last;

var justin = new Person("Justin", "Meyer");
justin.first; // "Justin"
justin.last; // "Meyer"

Then we'll extend Person into Programmer and add a favorite language:

Person("Programmer", {
    init: function(first, last, language) {
        // call base's init
        Person.prototype.init.apply(this, arguments);

        // other initialization code
        this.language = language;
    bio: function() {
        return 'Hi! I'm ' + this.first + ' ' + this.last +
            ' and I write ' + this.language + '.';

var brian = new Programmer("Brian", "Moschel", 'ECMAScript');
brian.bio(); // "Hi! I'm Brian Moschel and I write ECMAScript.";

Be Aware

can.Construct.prototype.setup is able to modify the arguments passed to init. If you aren't receiving the right arguments to init, check to make sure that they aren't being changed by setup somewhere along the inheritance chain.