Javascript – print all methods of an object

To print all methods of an object, we can use Object.getOwnPropertyNames and check if the corresponding value is a function. The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object. Here are some code snippets.

Print all methods of Array

var classobj = Array;
console.log(Object.getOwnPropertyNames(classobj).filter(function (x) {
  return typeof classobj[x] === 'function'
}));
[ 'isArray', 'from', 'of', 'observe', 'unobserve' ]
Env: node version v5.12.0

Print all methods of Array.prototype

var classobj = Array.prototype;
console.log(Object.getOwnPropertyNames(classobj).filter(function (x) {
  return typeof classobj[x] === 'function'
}));
Env: node version v5.12.0

Note that Array.prototype properties are different from Array properties.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments