3

I'm trying to do call the [[Prototype]] method of an object, by using this tutorial, from Mozilla (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain), but it does not seem to work.

Does anyone knows why?

Thanks,

Some code:

function Graph() {
  this.vertices = [];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function(v) {
    this.vertices.push(v);
  }
};

var g = new Graph();
g.[[Prototype]];
P.S.
  • 15,970
  • 14
  • 62
  • 86
Johann Gomes
  • 3,813
  • 5
  • 23
  • 25
  • 2
    The tutorial clearly says: _the `[[Prototype]]` is accessed using the accessors `Object.getPrototypeOf()` and `Object.setPrototypeOf()`. This is equivalent to the JavaScript property `__proto__` which is non-standard but de-facto implemented by many browsers._ – ibrahim mahrir Sep 30 '17 at 20:50
  • 1
    Because it's invalid syntax. – Bergi Sep 30 '17 at 20:51
  • ... so to access the prototype use the `Object.getPrototypeOf(g)` or `g.__proto__` not `g.[[Prototype]]` which actually should throw an error. – ibrahim mahrir Sep 30 '17 at 20:52
  • See also https://stackoverflow.com/q/28916710/1048572 or https://stackoverflow.com/q/33075262/1048572 – Bergi Sep 30 '17 at 20:56

1 Answers1

2

Because you trying to access prototype wrong way, it should be g.__proto__ (but you should never use g.__proto__, it's deprecated and does not work on all objects) or Object.getPrototypeOf(g):

function Graph() {
  this.vertices = [];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function(v) {
    this.vertices.push(v);
  }
};

var g = new Graph();
console.log(g.__proto__); // get access to prototype
console.log(Object.getPrototypeOf(g)); // get access to prototype

UPDATE

"I would like to execute something live new Graph(3), and expect the vertices to have 3 in the array."

Just use g.addVertex(3); to invoke addVertex function with parameter 3 (it will change only g.vertices array). Here is the example:

function Graph() {
  this.vertices = [];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function(v) {
    this.vertices.push(v);
  }
};

var g = new Graph();
g.addVertex(3);
console.log(g.vertices);

But you can pass parameter to constructor as well, like this:

function Graph(param) {
  this.vertices = [param];
  this.edges = [];
}

Graph.prototype = {
  addVertex: function(v) {
    this.vertices.push(v);
  }
};

var g = new Graph(1);
var h = new Graph(2);
console.log(g.vertices);
console.log(h.vertices);
P.S.
  • 15,970
  • 14
  • 62
  • 86
  • You should never use `g.__proto__`. It's deprecated and does not work on all objects! – Bergi Sep 30 '17 at 20:56
  • @Bergi, didn't know about that, thanks! I'll correct my answer – P.S. Sep 30 '17 at 20:57
  • Thanks, @Commercial Suicide. How to call the function addVertex with some argument, exactly? Since it's a constructor, I would like to execute something live `new Graph(3)`, and expect the vertices to have 3 in the array. What is the syntax? – Johann Gomes Sep 30 '17 at 21:01