class vs instance methods

Prev Top Next
var Person = function(firstlast){ 
 this.first = first
 this.last = last
}; 
 
// instance method 
Person.prototype.fullName = function(){ 
 return this.first + ' ' + this.last
}; 
 
// 'class' method 
Person.newKid = function(firstparent1, parent2){ 
 var last = parent1.last + '-' + parent2.last
 return new Person(firstlast); 
}; 
 
 
var jane = new Person('Jane''Gordon'); 
var dennis = new Person('Dennis''Levitt'); 
 
jane.newKid// ?// undefined -- class method isn't available on an instance 
Person.fullName// ?// undefined -- instance method isn't available on the class 
 
var joseph = Person.newKid('Joseph'janedennis); 
 
joseph instanceof Person// ?// true -- the factory method returns a new instance 
joseph.fullName(); // ?// 'Joseph Gordon-Levitt' -- assigns the name correctly