DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
  • Infrastructure
    • can-construct
      • prototype
        • constructor
        • init
        • setup
      • static
        • constructorExtends
        • extend
        • newInstance
        • setup
        • shortName
    • can-control
    • can-event
    • can-event/async/async
    • can-event/batch/batch
    • can-observation
    • can-simple-map
    • can-util
    • can-view-callbacks
    • can-view-live
    • can-view-model
    • can-view-nodelist
    • can-view-parser
    • can-view-scope
    • can-view-target
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Infrastructure
  • /
  • can-construct
  • / On this page
    • can-construct

      module

      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.

      • npm package badge
      • Star
      • source

      new Construct( ...args )

      Creates a new instance using Construct's constructor functions.

      Parameters

      1. 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.

      var Construct = require("can-construct");
      var 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.

      var 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.

      var 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):

      var 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.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.