Nasledjivanje objekata

by Daman Daman

JavaScript

function Person(){}
Person.prototype.dance = function(){};

function Ninja(){}

// Achieve similar, but non-inheritable, results
Ninja.prototype = Person.prototype;
Ninja.prototype = { dance: Person.prototype.dance };

// "Will fail with bad prototype chain." );
console.log((new Ninja()) instanceof Person)

// Only this maintains the prototype chain
Ninja.prototype = new Person();

var ninja = new Ninja();
// "ninja receives functionality from the Ninja prototype" );
console.log(ninja instanceof Ninja)
// "... and the Person prototype" );
console.log(ninja instanceof Person)
// "... and the Object prototype" );
console.log(ninja instanceof Object)