Oli Soproni B.
Oli Soproni B.

Reputation: 2800

List All Prototype Properties of a Javascript Object

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?

Upvotes: 35

Views: 28244

Answers (4)

Ahmad ghoneim
Ahmad ghoneim

Reputation: 929

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']

Upvotes: 0

gignu
gignu

Reputation: 2485

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!

Upvotes: 3

Bergi
Bergi

Reputation: 664433

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"

Upvotes: 70

Amadan
Amadan

Reputation: 198324

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.

Upvotes: 11

Related Questions