javascript - Add prototypes from one object to another -


let's have object cat in node module. want replace cat function , not it's prototypes. put way want take prototoypes 1 object , add them another.

function cat(name, breed){   this.name = name   this.breed = breed }  cat.prototype.sayname = function(){   console.log(this.name) }  cat.prototype.saybreed = function(){   console.log(this.breed) }  module.export = cat 

then have file:

var _ = require("underscore") var inherit = require('util').inherits; var cat = require("./cat")  function dog(name, breed){   this.name = name   this.breed = breed }  // tries: // _.extend(dog, cat) // logs: {} // inherit(dog, cat) // error: super constructor `inherits` must have prototype. // dog.prototype.sayname = cat.prototype.sayname // cannot read property 'sayname' of undefined // dog.prototype.saybreed = cat.prototype.saybreed   var dog = new dog("wilmer", "huskey") console.log(dog.__proto__) 

how can import / extend / inherit of prototypes cat dog?

this should work:

 _.extend(dog.prototype, cat.prototype); 

so in code can do:

var _ = require("underscore") var cat = require("./cat")  function dog(name, breed){   this.name = name   this.breed = breed }  _(dog.prototype).extend(cat.prototype);  var dog = new dog("wilmer", "huskey");  dog.sayname();  // => "wilmer" dog.saybreed(); // => "huskey" 

Comments