2012-02-25 16 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 
    } 
    }); 
}; 

誰かが私にこの機能がどのように動作するかの「全体像」をお願いできますか? 私はObject.createを取得していることを100%確信していません、そして、...まあ、私は混乱しています!

ヘルプ/ポインターは大変ありがとうございます。

メル。

答えて

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

-1

継承機能には2つのバージョンがあります。 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

関連する問題