【问题标题】:node.js, require() and prototypal-inheritancenode.js、require() 和原型继承
【发布时间】:2012-06-05 18:38:12
【问题描述】:

我对 node.js 很陌生,可能还有 javascript,所以请随时指出任何看起来很尴尬的地方。我是来学习的。

这是我想要做的:

  • “John”对象应继承“Client”对象的所有内容,包括函数
  • 在实例化新对象和继承时使用 Object.create() 而不是“new”关键字

这是一个有效的单文件测试:

var sys=require('sys');
var Client = {
  ip: null,
  stream : null,
  state: "Connecting",
  eyes: 2,
  legs: 4,
  name: null,
  toString: function () {
      return this.name + " with " +
      this.eyes + " eyes and " +
      this.legs + " legs, " + this.state + ".";
  }
}
var john = Object.create(Client, {
  name: {value: "John", writable: true},
  state :  {value: "Sleeping", writable: true}
}); 
sys.puts(john); // John with 2 eyes and 4 legs, Sleeping

当我将其拆分为不同的文件时会发生以下情况:

---- client.js ----

module.exports = function (instream, inip){
    return  {
         ip: inip,
          stream : instream,
          state: "Connecting",
          eyes: 2,
          legs: 4,
          name: null,
          toString: function () {
            return this.name + " with " +
              this.eyes + " eyes and " +
              this.legs + " legs, " + this.state + ".";
          },
    };

}; 

---- john.js ----

var Client = require("./client");
module.exports = function (inname, instate){
    return Object.create(Client, {
      state : {value: inname, enumerable: false, writable: true},
      name: {value: instate, enumerable: true, writable: true},
    });

};

---- main.js ----

var sys = require("util");
var Client = require("./client")("stream","168.192.0.1");
sys.puts(Client); // null with 2 eyes and 4 legs, Connecting

var john = require("./john")("John","Sleeping");
sys.puts(john); //Function.prototype.toString no generic 
sys.puts(sys.inspect(john)); // { name: 'Sleeping' }
sys.puts(sys.inspect(john, true)); // {name: 'Sleeping', [state]: 'John'}

问题:

  1. 我在拆分文件和使用导致问题的 require() 时做错了什么?
  2. 为什么 john 对象的名称为“Sleeping”,状态为“John”?我知道这是我放置行的顺序,但它不应该遵循我在构造函数中放置的参数吗?
  3. 有更好的方法吗?我倾向于学习 Object.create() 而不是依赖“new”关键字。

【问题讨论】:

  • 这看起来像是一个语法错误。 client.js 中的导出函数缺少 return {}; 的行。
  • 哎呀,你是对的。现在修好了。它在文件中,只是没有正确剪切和粘贴。

标签: javascript node.js require prototypal-inheritance


【解决方案1】:

在#2:

return Object.create(Client, {
  state : {value: inname, enumerable: false, writable: true},
  name: {value: instate, enumerable: true, writable: true},
});

这只是您在交换innameinstate 时的简单错误。

在#1:

我怀疑原始单文件代码和新的 3 文件代码之间的另一个细微差别有问题。 MDN's page on Object.create 上的一条特别评论引起了我的注意,具体来说,

当然,如果Constructor函数中有实际的初始化代码,Object.create就无法体现出来

您的client.js 文件正在生成一个构造函数。您在john.js 中的意思是(结合#1 和#2):

return Object.create(Client(), {
  state : {value: instate, enumerable: false, writable: true},
  name: {value: inname, enumerable: true, writable: true},
});

调用客户端函数,使其返回一个对象而不是一个函数,然后在此基础上创建一个新对象。

在#3:

我不明白你为什么不能使用这种模式,但你刚刚证明它是:

  1. 与当今大多数 Javascript 对象的创建方式不同(尽管毫无疑问是一种更快的构造机制)。
  2. 使用较新的语法(删除 new),这样语法错误就不会像使用旧的 Java 样式语法那样“跳出”。

请记住这一点。不过,我很高兴你问了这个问题,因为现在我知道了 Object.create。 :)

【讨论】:

  • 我对错误输入感到非常尴尬。 =^^=感谢您的关注!现在一切正常,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 2013-05-20
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多