List All Prototype Properties of a Javascript Object
Asked Answered
D

4

35

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?

Downes answered 11/5, 2015 at 1:38 Comment(0)
I
69

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"
Iey answered 11/5, 2015 at 1:46 Comment(2)
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]}); Stepper
@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(…)Iey
G
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.

Grantham answered 11/5, 2015 at 1:46 Comment(1)
Note that this will only print enumerable properties, whereas the accepted answer will print all properties, enumerable or not.Lyndel
C
3

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!

Cordiality answered 5/4, 2022 at 15:3 Comment(0)
N
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']
Negligible answered 28/6, 2023 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.