关于设置something.prototype .__ proto__的困惑 [英] Confusion about setting something.prototype.__proto__

查看:91
本文介绍了关于设置something.prototype .__ proto__的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js的Express模块​​的代码中,我遇到了这一行,设置了服务器的继承:

  Server.prototype .__ proto__ = connect.HTTPServer.prototype; 

我不确定这是做什么的 - MDC文档(https://developer.mozilla。 org / en / JavaScript / Guide / Inheritance_Revisited #prototype_and_ proto )似乎说我可以做:

  Server.prototype = connect.HTTPServer.prototype; 

确实,我做了这个测试:

  var parent = function(){} 
parent.prototype = {
test:function(){console.log('test')};
}

var child1 = function(){};
child1.prototype = parent.prototype;
var instance1 = new child1();
instance1.test(); //'test'

var child2 = function(){};
child2.prototype .__ proto__ = parent.prototype;
var instance2 = new child2();
instance2.test(); //'test'

看起来是一样的吗?所以,是的,我想知道设置object.prototype .__ proto是为了什么。谢谢!

解决方案

查看此页面(mckoss.com)显示原型构造函数 __ proto __ 小层次结构的关系。此外,图中的代码也很好地描述了这种关系。



当你有一个函数 Base ,并设置定义的函数对象的原型,语句 Derived.prototype = new Base; 设置 __ proto __ (实际上是内部 [[prototype]] Derived.prototype Base.prototype 自动使Derived本身成为可以实例化对象的类。这似乎是一种更符合标准的定义派生类的方法。



从我读到的, __ proto __ 是访问对象的内部 [[prototype]] 的非标准方式。它似乎得到了很好的支持,但我不确定它是否应该被信任。



在任何情况下,你的例子 Server.prototype .__ proto__ = connect.HTTPServer.prototype; 似乎以相反的方式进行派生:首先通过定义构造函数和proto来定义一个对象 Server ,然后手动挂接内部 [[prototype]] ,将其转换为派生自 HTTPServer 的类。 / p>

至于你建议的替代方案, Server.prototype = connect.HTTPServer.prototype; :这是一个坏主意。在这里,您将 Server 的原型设置为与 HTTPServer 的原型相同的对象。因此,您对 Server 类所做的任何更改都将直接反映在 HTTPServer 中,并且可以从其他派生类中进行更改。 的HttpServer 。如果从 HTTPServer 派生的两个类尝试定义相同的成员,则可以对混乱进行映像。


In the code for the Express module for Node.js I came across this line, setting inheritance for the server:

Server.prototype.__proto__ = connect.HTTPServer.prototype;

I'm not sure what this does - the MDC docs (https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited#prototype_and_proto) seem to say that I could just do:

Server.prototype = connect.HTTPServer.prototype;

Indeed, I did this test:

var parent = function(){}
parent.prototype = {
    test: function(){console.log('test')};
}

var child1 = function(){};
child1.prototype = parent.prototype;
var instance1 = new child1();
instance1.test();     // 'test'

var child2 = function(){};
child2.prototype.__proto__ = parent.prototype;
var instance2 = new child2();
instance2.test();     // 'test'

Looks to be the same? So yah, I'm wondering what setting object.prototype.__proto is for. Thanks!

解决方案

Have a look at the diagram on this page (mckoss.com) that shows the prototype, constructor, __proto__ relations for a small hierarchy. Also the code below the diagram describes the relation quite well.

When you have a function Base, and set the prototype of the function object defined, the statement Derived.prototype = new Base; sets the __proto__ (actually the internal [[prototype]]) of Derived.prototype to Base.prototype automatically, making Derived itself a class that you can instantiate objects from. This seems the be a more standards compliant way of defining a derived class.

From what I read, __proto__ is a non-standard way of accessing the internal [[prototype]] of an object. It seems to be well supported, but I am not sure if it should be trusted.

In any case, your example Server.prototype.__proto__ = connect.HTTPServer.prototype; seems to do the derivation the other way around: first define an object, Server by defining the constructor and the proto, and then hook up the internal [[prototype]] manually to morph it into a class derived from HTTPServer.

As for your suggested alternative, Server.prototype = connect.HTTPServer.prototype;: that is a bad idea. Here, you are setting the prototype of Server to be the same object as the prototype of HTTPServer. So any changes you make to Server class will be directly reflected in HTTPServer, and will be accessible from other derived classes of HTTPServer. You can imageine the chaos if two classes derived from HTTPServer try to define the same member.

这篇关于关于设置something.prototype .__ proto__的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆