newInstance

  • function
can.Construct.newInstance  

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

can.Construct.newInstance([...args])

Parameters

  1. args {*}Optional

    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 = can.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