Na czym polega __proto__ i prototype?

0

Czesc, na czym dokladnie to polega w Jsi-e? Jak jest roznica i do czego mozna to stosowac? Nie moge tego za cholere zaczaic.

0

Ogladalem to i nie trybie tego. Moze dlatego ze po robocie, nie wiem, ale nie moge tego zaczaic. I w czym to sie wykorzystuje w ogole.

1

Mysle, ze to wszystko wyjasnia:

They're very much not the same, but there is a relationship between them. They're both sort-of unfortunately named.

Short answer: 
__proto__ is the actual prototype, but don't use it.
.constructor.prototype was supposed to do the same thing as __proto__ but its mostly broken.
A function's .prototype is actually the prototype of things made by it, not its prototype.

Now the long answer: 
In prototype-based object oriented languages like Self and Javascript, every object (i.e., instance) in the system has a (potentially anonymous) field that says "if I [the object] don't have a property or method that is requested of me, go to the object that this field references - my prototype - and look for it". Since that object will have this field as well, this becomes a recursive process and it is what is meant by a "prototype chain." Note that this means that in a prototype language, there is no abstract concept of a "class" - an object's "api" is just determined by the transitive closure over this field.

Javascript originally completely hid this field from the programmer, but Mozilla exposed it with .__proto__. So you could actually take any object, say it was of type Foo, and set __proto__ so that it would now be of type Bar. Which is weird. But technically __proto__ is actually the "prototype" of that object.
Now here's the confusing part. To make its prototype system look more like classical-inheritance OO, Javascript kind-of readopted the idea of constructors and classes: every function is eligible to be a constructor by calling it with the new keyword, and by setting the .prototype property of that function, you tell Javascript that "when this function is used as a constructor, set up the object's prototype-ish field to this object." So that's not actually the prototype of the function - its prototype is, of course, Function.prototype.

The original idea was that you could get the kind of thing that __proto__ referred to by taking an object and getting .constructor.prototype. But .constructor itself succumbs to prototypal inheritance, so unless the object's constructor set itself up to be that value, that wouldn't work. And nobody did that, so it's mostly broken.

As was mentioned, ES5 is kind-of rectifying this with functions like Object.isPrototypeOf and Object.getPrototypeOf which will always work.

Źródło: http://www.quora.com/What-is-the-difference-between-__proto__-and-prototype

dodanie znacznika <code> w znaczniku <quote> - @furious programming

1 użytkowników online, w tym zalogowanych: 0, gości: 1