34

Is there any other way to look up for the prototype properties of an javascript object. Lets say I have like this.

function proton() {
    this.property1 = undefined;
    this.property2 = undefined;
};

proton.prototype = {

    sample1 : function() {
        return 'something';
    },

    sample2 : function() {
        return 'something';
    }

};

var my_object = new proton();

console.log(Object.keys(my_object));

returns ["property1", "property2"]

console.log(Object.getOwnPropertyNames(my_object));

returns ["property1", "property2"]

But what i want to print is the prototype properties of the object my_object.

['sample1', 'sample2']

In order for me to see the prototype properties of that object i need to console.log(object) and from developer tools i can look up for the properties of that object.

But since I am using third party libraries like phaser.js, react.js, create.js so i don't know the list of the prototype properties of a created object from this libraries.

Is there any prototype function of Object to list down all the prototpye properties of a javascript object?

gignu
  • 1,763
  • 1
  • 14
  • 24
Oli Soproni B.
  • 2,774
  • 3
  • 22
  • 47

4 Answers4

67

Not a prototype method, but you can use Object.getPrototypeOf to traverse the prototype chain and then get the own property names of each of those objects.

function logAllProperties(obj) {
     if (obj == null) return; // recursive approach
     console.log(Object.getOwnPropertyNames(obj));
     logAllProperties(Object.getPrototypeOf(obj));
}
logAllProperties(my_object);

Using this, you can also write a function that returns you an array of all the property names:

function props(obj) {
    var p = [];
    for (; obj != null; obj = Object.getPrototypeOf(obj)) {
        var op = Object.getOwnPropertyNames(obj);
        for (var i=0; i<op.length; i++)
            if (p.indexOf(op[i]) == -1)
                 p.push(op[i]);
    }
    return p;
}
console.log(props(my_object)); // ["property1", "property2", "sample1", "sample2", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable"
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Not a JS guru here, is there any reason why accessing `__proto__` and converting it to an array is a bad idea? ex: `var arr = Object.keys(my_object.__proto__).map(function (key) {return my_object.__proto__[key]}); ` – Bassem May 11 '15 at 02:00
  • 2
    @BassemDy For one, `.__proto__` is just like `Object.getPrototypeOf` but deprecated and doesn't work in a few edge cases. Also, accessing only one level of the prototype chain might not get you all properties. Btw, OP seems to be interested in property names not the values, so you can omit that `.map(…)` – Bergi May 11 '15 at 02:02
11
function prototypeProperties(obj) {
  var result = [];
  for (var prop in obj) {
    if (!obj.hasOwnProperty(prop)) {
      result.push(prop);
    }
  }
  return result;
}

EDIT: This will grab all the properties that were defined on any ancestor. If you want a more granular control of what is defined where, Bergi's suggestion is good.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 4
    Note that this will only print *enumerable* properties, whereas the accepted answer will print *all* properties, enumerable or not. – Ohad Schneider Dec 29 '16 at 13:28
2

The quick and dirty one-liner solution would be:

console.log(Object.getOwnPropertyNames(Object.getPrototypeOf({ prop1: 'val1' })))

If you want something more precise, go with the accepted answer!

gignu
  • 1,763
  • 1
  • 14
  • 24
0

If you want to List All Prototype Properties of an Object.

let obj = {prop1:"", method(){}}
console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(obj)))
// (12) ['constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty', '__lookupGetter__', '__lookupSetter__', 'isPrototypeOf', 'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString']

and if you want all properties including the object's own properties.

[ ...Object.getOwnPropertyNames( Object.getPrototypeOf(obj) ), ...Object.getOwnPropertyNames(obj) ]

// (14) ['constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty', '__lookupGetter__', '__lookupSetter__', 'isPrototypeOf', 'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString', 'prop1', 'method']

object's own properties only.

Object.getOwnPropertyNames(obj)
// (2) ['prop1', 'method']
Ahmad ghoneim
  • 844
  • 7
  • 13