setup

  • function
can.Construct.setup  

Perform initialization logic for a constructor function.

can.Construct.setup(base, fullName, staticProps, protoProps)

A static setup method provides inheritable setup functionality for a Constructor function. The following example creates a Group constructor function. Any constructor functions that inherit from Group will be added to Group.childGroups.

Group = can.Construct.extend({
  setup: function(Construct, fullName, staticProps, protoProps){
    this.childGroups = [];
    if(Construct !== can.Construct){
      this.childGroups(Construct)
    }
    Construct.setup.apply(this, arguments)
  }
},{})
var Flock = Group.extend(...)
Group.childGroups[0] //-> Flock

Parameters

  1. base {constructor}

    The base constructor that is being inherited from.

  2. fullName {String}

    The name of the new constructor.

  3. staticProps {Object}

    The static properties of the new constructor.

  4. protoProps {Object}

    The prototype properties of the new constructor.

The static setup method is called immediately after a constructor function is created and set to inherit from its base constructor. It is useful for setting up additional inheritance work. Do not confuse this with the prototype setup method.

Setup Extends Defaults

Setup deeply extends the static defaults property of the base constructor with properties of the inheriting constructor. For example:

Parent = can.Construct.extend({
  defaults : {
    parentProp: 'foo'
  }
},{})

Child = Parent.extend({
  defaults : {
    childProp : 'bar'
  }
},{}

Child.defaults // {parentProp: 'foo', 'childProp': 'bar'}

Example

This Parent class adds a reference to its base class to itself, and so do all the classes that inherit from it.

Parent = can.Construct.extend({
  setup : function(base, fullName, staticProps, protoProps){
    this.base = base;

    // call base functionality
    can.Construct.setup.apply(this, arguments)
  }
},{});

Parent.base; // can.Construct

Child = Parent({});

Child.base; // Parent