Confusion in installing something. Prototype .__ proto__

In the code for the Express module for Node.js, I came across this line, setting the 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' 

It seems the same thing? So yes, I am wondering what parameter object.prototype .__ proto is for. Thank!

+11
javascript
Mar 22 '11 at 10:10
source share
2 answers

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

When you have a Base function and a function object prototype is given, the Derived.prototype = new Base; statement Derived.prototype = new Base; automatically sets __proto__ (actually internal [[prototype]] ) Derived.prototype in Base.prototype make Produces the class itself from which you can create objects. This seems to be a more standard way to define a derived class.

From what I'm reading, __proto__ is a non-standard way of accessing an internal [[prototype]] object. This seems to be well supported, but I'm not sure that he should be trusted.

In any case, your example Server.prototype.__proto__ = connect.HTTPServer.prototype; , it seems, draws the opposite conclusion: first define the object, Server , defining the constructor and proto, and then manually connect the internal [[prototype]] to morphine this into the class obtained from HTTPServer .

As for the alternative you proposed, Server.prototype = connect.HTTPServer.prototype; : it is a bad idea. Here you install the Server prototype for the same object as the HTTPServer prototype. Therefore, any changes you make to the Server class will be directly reflected in the HTTPServer and will be available from other derived HTTPServer classes. You can portray chaos if two classes derived from HTTPServer try to define the same element.

+10
Mar 22 2018-11-22T00:
source share

The __proto__ custom property allows you to set up a prototype of an existing object.

In your example, both versions will achieve the same effect, but there is a difference:

Prototype

child1 is the same as the parent prototype, while the child2 prototype is an empty object, and this empty object prototype is the same as the parent prototype.

Of course, like child2 , and its prototype does not have a test method, this method will be discussed later in the prototype chain.

Also consider the following:

You want to create only one object, which should inherit from another object. Now you can write a constructor, but JavaScript has object notation for creating objects directly and you want to use it.

If you have a constructor function, then allowing new objects to inherit from another object is just as easy to configure the prototype of the constructor function on that object.

Obviously, this does not work for object literals. But in Firefox, you can use __proto__ to install it:

 var server = { __proto__: connect.HTTPServer.prototype, other: properties }; 



Since this property is not standard, it should be avoided.

+2
Mar 22 '11 at 22:28
source share



All Articles