What is the difference between __proto__
and prototype
I read most of articles in the web and I still cannot understand it..
as far as I understand
__proto__
is the property which is for prototype object
prototype
is the actual object
am I correct? ....
Why only functions have prototype property? And how is it be an object?
var fn = function(){};
console.dir(fn);
output
function fn() arguments: null caller: null length: 0 name: "" prototype: Object __proto__: () <function scope>
Using object and function I try to set values for __proto__
and prototype in chrome console as shown below
//create object and display it
var o = {name : 'ss'};
console.dir(o);
output
Object name: "ss", __proto__: Object
//set the values
o.__proto__ = 'aaa';
o.prototype = 'bbb';
//after set the values display the object
console.dir(o);
output
Object name: "ss", prototype: "aaa", __proto__: Object
//create function and display it
var fn = function(){};
console.dir(fn);
output
function fn() arguments: null caller: null length: 0 name: "" prototype: Object __proto__: () <function scope>
//set the values
fn.prototype = 'fff';
fn.__proto__ = 'eee';
//after set the values display the object
console.dir(fn);
output
function fn() arguments: null caller: null length: 0 name: "" prototype: "fff" __proto__: function() <function scope>
Then I realize that I can't set values for __proto__
but can set values to prototype
. W why can't I set values for __proto__
???
The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.
The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.
In reality, the only true difference between prototype and __proto__ is that the former is a property of a class constructor, while the latter is a property of a class instance.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
That's pretty simple actually.
{object}.__proto__
is a reference to {constructor function}.prototype
object.new {constructor function} (params)
in JavaScript does three major things:
{constructor function}
with this
set to that newborn object (obj).obj.__proto__ = {constructor function}.prototype;
And that's pretty much it.
That obj.__proto__
establishes single linked list used to lookup for properties not defined in the object itself. As it is set to {constructor function}.prototype
object then we can treat that prototype as a "rack" for object methods - functions associated with object instances.
Example:
function Foo() {}
Foo.prototype.bar = function() { return "foo.bar here"; }
var obj = new Foo(); // creating object with __proto__ set to Foo.prototype;
obj.bar(); // will return "foo.bar here"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With