73

In all the articles it is written that JavaScript is a prototype-based language, meaning that every object has a prototype (or, more precisely, prototype chain).

So far, I've tried the following code snippet:

var F = function();
F.prototype.member1 = 1;
var object1 = new F();
console.log(object1.member1); // prints 1

How can I access the prototype object of object1? Is there a browser-neutral way to do that (I mean, not relying on __proto__ property? Seen this link, but maybe there are new developments since 2010) If I can't, could you share please the rationale behind the hood?

Community
  • 1
  • 1
BreakPhreak
  • 10,940
  • 26
  • 72
  • 108

5 Answers5

126
var f = function();
var instance = new f();

If you know name of instance class function, you can simply access prototype as:

var prototype = f.prototype;
prototype.someMember = someValue;

If you don't:

1)

var prototype = Object.getPrototypeOf(instance);
prototype.someMember = someValue;

2) or

var prototype = instance.__proto__;
prototype.someMember = someValue;

3) or

var prototype = instance.constructor.prototype; // works only if constructor is properly assigned and not modified
prototype.someMember = someValue;

For compatibility you can place into your code the following snippet (and use always Object.getPrototypeOf(instance) to return prototype):

if(!Object.getPrototypeOf) {

  if(({}).__proto__ === Object.prototype && ([]).__proto__ === Array.prototype) {

    Object.getPrototypeOf = function getPrototypeOf(object) {
      return object.__proto__;
    };

  } else {

    Object.getPrototypeOf = function getPrototypeOf(object) {

      // May break if the constructor has been changed or removed
      return object.constructor ? object.constructor.prototype : void 0;

    };

  }
}

UPDATE:

According to ECMA-262 6th Edition (June 2015) __proto__ property is standardized as additional feature for Web browsers. All latest editions of top browsers supports it now. Read more about __proto__:

Ivan
  • 34,531
  • 8
  • 55
  • 100
Andrew D.
  • 8,130
  • 3
  • 21
  • 23
  • with a single comment though: `__proto__` won't work in every browser – BreakPhreak Oct 06 '11 at 14:39
  • 15
    Not only `__proto__` but whole above three methods won't work in all browsers. But compatibility code is based on all these methods. Additionaly, for five major browsers (IE,FF,Chrome,Safari,Opera) all last editions of browsers have support for `Object.getPrototypeOf()`, and compatibility code needs only for IE8 and older. – Andrew D. Oct 07 '11 at 05:12
10

It looks like

Object.getPrototypeOf(passedObject);

will work for this, and is compatible with modern browsers.

Here are the compatibility tables on MDN

HelloWorld
  • 2,480
  • 3
  • 28
  • 45
6
var F = function(){};
var object1 = new F();
alert(object1.constructor === F);
alert(object1.constructor.prototype === F.prototype);
  • 2
    Note that sometimes it does not work. For example a = Function.prototype; a.constructor.prototype will be equal to a. But obviously prototype of a is different. You can confirm that the prototype of a is Object.prototype by checking a.__proto__ which is the best way to check the prototype. – Piotr Dabkowski Oct 30 '14 at 21:23
2
var F = function();
F.prototype.member1 = 1;
F.prototype.getClass = F;

var object1 = new F();
object1.member1 = 2;

console.log(object1.getClass.prototype.member1); // prints 1
console.log(object1.member1); // prints 2
r3mark
  • 506
  • 5
  • 12
-1
var F = function();
F.prototype.member1 = 1;
var intance = new F();
console.log(instance['member1']);
Basel Issmail
  • 3,847
  • 7
  • 20
  • 36
Kado
  • 1
  • Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/22981161) – Nick May 11 '19 at 02:39