【问题标题】:How does the inherits function work in Node.js?继承函数在 Node.js 中是如何工作的?
【发布时间】:2012-02-25 03:18:14
【问题描述】:

这是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 ,而且……我很困惑!

任何帮助/指针将不胜感激...

佣兵。

【问题讨论】:

    标签: javascript inheritance node.js


    【解决方案1】:
    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

    【讨论】:

    • 不确定它值多少钱,但是……嗯,谢谢。我正在重新阅读我的问题 - 以及答案 - 过了一会儿,当我终于“明白”这一切时,我不得不说,现在一切看起来都非常简单......我想是时候了去这里帮助别人!
    【解决方案2】:

    现在有两个版本的继承函数。 pre node.js version 5.0.0 使用 Object.create 和 post (inclusive) 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多