Instance Type

1305 ワード

Examining the basics of an object.
 function Ninja(){} 
  
 var ninja = new Ninja(); 

 assert( typeof ninja == "object", "However the type of the instance is still an object." );   
 assert( ninja instanceof Ninja, "The object was instantiated properly." ); 
 assert( ninja.constructor == Ninja, "The ninja object was created by the Ninja function." ); 
We can still use the constructor to build other instances.
 function Ninja(){}
 var ninja = new Ninja(); 
 var ninjaB = new ninja.constructor(); 
  
 assert( ninjaB instanceof Ninja, "Still a ninja object." ); 
QUIZ: Make another instance of a Ninja.
var ninja = (function(){
  function Ninja(){}
  return new Ninja();
})();

// Make another instance of Ninja
var ninjaB = ___;

assert( ninja.constructor == ninjaB.constructor, "The ninjas come from the same source." );
QUIZ: Use the .constructor property to dig in.
var ninja = (function(){
  function Ninja(){}
  return new Ninja();
})();

// Make another instance of Ninja
var ninjaB = new ninja.constructor();

assert( ninja.constructor == ninjaB.constructor, "The ninjas come from the same source." );