2012-02-25 40 views
2

這是繼承功能的node.js:繼承函數如何在Node.js中工作?

exports.inherits = function(ctor, superCtor) { 
    ctor.super_ = superCtor; 
    ctor.prototype = Object.create(superCtor.prototype, { 
    constructor: { 
     value: ctor, 
     enumerable: false, 
     writable: true, 
     configurable: true 
    } 
    }); 
}; 

有人可以請給我的這個功能是如何工作的「大局」? 我不是100%確定我得到Object.create,而且......我很困惑!

任何幫助/指針將不勝感激...

Merc。

回答

3
var Parent = function() { 
    this.isParent = true; 
}; 

Parent.prototype.doSomething = function() { 
    return 42; 
}; 

var Child = function() { 
    this.isChild = true; 
} 
util.inherits(Child, Parent); 

var c = new Child; 
console.log(c.isChild); 
console.log(c.doSomething()); 

它只是確保了Child.prototype繼承Parent.prototype正常。它還將constructor屬性Child.prototype設置爲正確的值。

請確保在您的Child聲明後直接致電util.inherits

+0

不知道它的價值多少繼承的作品,但......嗯,謝謝你詳細的解釋。我正在重讀我的問題 - 答案 - 稍後,當我終於「得到」所有這些,我不得不說,現在看起來這一切非常簡單......我想現在是時候了去這裏幫助別人! – Merc 2012-03-19 03:41:31

-1

現在有兩個版本的繼承函數。 Pre node.js版本5.0.0使用Object.create和post(包含)v5.0.0使用Object.setPrototypeOf。

在Object.create版本中,superCtor的原型被設置爲ctor.prototype的原型。但是,在此過程中,ctor.prototype上的任何方法/屬性都將被刪除。這是一個工作的例子;

var inherits = function (ctor, superCtor) { 
 
    ctor.super_ = superCtor; 
 
    ctor.prototype = Object.create(superCtor.prototype, { 
 
     constructor: { 
 
      value: ctor, 
 
      enumerable: false, 
 
      writable: true, 
 
      configurable: true 
 
     } 
 
    }); 
 
}; 
 

 
function Manager(managerName) { 
 
    this.managerName = managerName; 
 
} 
 

 
Manager.prototype.getManagerName = function() { 
 
    return this.managerName; 
 
} 
 

 
function Team(managerName, teamName) { 
 
    this.teamName = teamName; 
 
    Team.super_.apply(this, arguments); 
 
} 
 

 

 
Team.prototype.getTeamDetails = function() { 
 
    return this.teamName + ' is managed by ' + this.managerName; 
 
} 
 

 
inherits(Team, Manager); 
 

 
var obj = new Team('Klopp', 'LiverpoolFC'); 
 

 
console.log(obj.getTeamDetails()); //obj.getTeamDetails is not a function

以上問題的Node.js 5.0.0版或更新就解決了。 util.inherits現在使用setPrototypeOf來解決這個問題。

var inherits = function (ctor, superCtor) { 
 
    ctor.super_ = superCtor; 
 
    ctor.prototype = Object.create(superCtor.prototype, { 
 
     constructor: { 
 
      value: ctor, 
 
      enumerable: false, 
 
      writable: true, 
 
      configurable: true 
 
     } 
 
    }); 
 
}; 
 

 
function Manager(managerName) { 
 
    this.managerName = managerName; 
 
} 
 

 
Manager.prototype.getManagerName = function() { 
 
    return this.managerName; 
 
} 
 

 
function Team(managerName, teamName) { 
 
    this.teamName = teamName; 
 
    Team.super_.apply(this, arguments); 
 
} 
 

 

 
Team.prototype.getTeamDetails = function() { 
 
    return this.teamName + ' is managed by ' + this.managerName; 
 
} 
 

 
inherits(Team, Manager); 
 

 
var obj = new Team('Klopp', 'LiverpoolFC'); 
 

 
console.log(obj.getTeamDetails()); //obj.getTeamDetails is not a function

此鏈接提供瞭如何在Node.js的 Node.js - Anatomy of inherits