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

      function

      Returns an instance of Construct. This method can be overridden to return a cached instance.

      • source

      Construct.newInstance([...args])

      Parameters

      1. args {*}:

        arguments that get passed to setup and init. Note that if setup returns an array, those arguments will be passed to init instead.

      Returns

      {class}:

      instance of the class

      Creates a new instance of the constructor function. This method is useful for creating new instances with arbitrary parameters. Typically, however, you will simply want to call the constructor with the new operator.

      Example

      The following creates a Person Construct and overrides newInstance to cache all instances of Person to prevent duplication. If the properties of a new Person match an existing one it will return a reference to the previously created object, otherwise it returns a new object entirely.

      // define and create the Person constructor
      var Person = Construct.extend({
        init : function(first, middle, last) {
          this.first = first;
          this.middle = middle;
          this.last = last;
        }
      });
      
      // store a reference to the original newInstance function
      var _newInstance = Person.newInstance;
      
      // override Person's newInstance function
      Person.newInstance = function() {
        // if cache does not exist make it an new object
        this.__cache = this.__cache || {};
        // id is a stingified version of the passed arguments
        var id = JSON.stringify(arguments);
      
        // look in the cache to see if the object already exists
        var cachedInst = this.__cache[id];
        if(cachedInst) {
          return cachedInst;
        }
      
        //otherwise call the original newInstance function and return a new instance of Person.
        var newInst = _newInstance.apply(this, arguments);
        this.__cache[id] = newInst;
        return newInst;
      };
      
      // create two instances with the same arguments
      var justin = new Person('Justin', 'Barry', 'Meyer'),
          brian = new Person('Justin', 'Barry', 'Meyer');
      
      console.log(justin === brian); // true - both are references to the same instance
      

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