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
  • /
  • init
  • / On this page
    • init

      function

      Called when a new instance of a Construct is created.

      • source

      construct.init(...args)

      Parameters

      1. args {*}:

        the arguments passed to the constructor (or the items of the array returned from setup)

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

      Examples

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

      var Person = Construct.extend({
          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:

      var Programmer = Person.extend({
          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.";
      

      Modified Arguments

      setup is able to modify the arguments passed to init. If you aren't receiving the arguments you passed to new Construct(args), check that they aren't being changed by setup along the inheritance chain.

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