Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting values to __proto__` and `prototype in javascript [duplicate]

Tags:

javascript

oop

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__ ???

like image 200
Susantha7 Avatar asked May 09 '16 04:05

Susantha7


People also ask

What is prototype and __ proto __ in JavaScript?

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.

What is the use of __ proto __ in JavaScript?

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.

What is the difference between __ proto __ Dunder Proto and prototype?

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.

What are differences between F prototype and `__ proto __`?

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.


1 Answers

That's pretty simple actually.

  1. {object}.__proto__ is a reference to {constructor function}.prototype object.
  2. operator new {constructor function} (params) in JavaScript does three major things:
    1. Creates new object, let it be 'obj'.
    2. Calls that {constructor function} with this set to that newborn object (obj).
    3. Sets 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"
like image 103
c-smile Avatar answered Oct 14 '22 21:10

c-smile