Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Down All Prototype Properties of an Javascript Object

Tags:

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?

like image 247
Oli Soproni B. Avatar asked May 11 '15 01:05

Oli Soproni B.


People also ask

How do you list all properties of an object in JS?

To get all own properties of an object in JavaScript, you can use the Object. getOwnPropertyNames() method. This method returns an array containing all the names of the enumerable and non-enumerable own properties found directly on the object passed in as an argument.

What is the prototype property in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

How many properties does a prototype object have in JavaScript?

prototype by default has one own property: constructor , which references the constructor function itself — that is, Box. prototype. constructor === Box .

What are the properties of an object in JavaScript?

Properties are the values associated with a JavaScript object. A JavaScript object is a collection of unordered properties. Properties can usually be changed, added, and deleted, but some are read only.


1 Answers

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" 
like image 162
Bergi Avatar answered Sep 20 '22 16:09

Bergi