简体   繁体   English

获取任何对象的所有方法?

[英]Get all methods of any object?

In python word there are dir() function which在 python 中,有 dir() 函数

return a list of valid attributes for that object返回该对象的有效属性列表

In JS words I found:在 JS 的话我发现:

Object.getOwnPropertyNames

Object.keys

but they don't show ALL attributes:但它们没有显示所有属性:

> Object.getOwnPropertyNames([])
[ 'length' ]

How to get list of all attributes and methods as如何获取所有属性和方法的列表

concat, entries, every, find.... 

for Array() for example?对于 Array() 例如?

You can use Object.getOwnPropertyNames with Object.getPrototypeOf in order to traverse the prototype chain and collect all own properties on each object.您可以使用Object.getOwnPropertyNamesObject.getPrototypeOf来遍历原型链并收集每个对象的所有属性。

 var result = [] var obj = [] do { result.push(...Object.getOwnPropertyNames(obj)) } while ((obj = Object.getPrototypeOf(obj))) document.querySelector("pre").textContent = result.join("\\n")
 <pre></pre>

This handles all properties irrespective of whether or not they're inherited or enumerable.这将处理所有属性,而不管它们是继承的还是可枚举的。 This does not include Symbol properties however.但是,这不包括Symbol属性。 To include those, you can use Object.getOwnPropertySymbols .要包含这些,您可以使用Object.getOwnPropertySymbols

var result = []
var obj = []
do {
  result.push(...Object.getOwnPropertyNames(obj), ...Object.getOwnPropertySymbols(obj))
} while ((obj = Object.getPrototypeOf(obj)))

Object.getOwnPropertyNames(Array.prototype)

The reason why trying to get the values the way you posted does not work, is because you are requesting the property names for a single instance of the Array object.尝试以您发布的方式获取值不起作用的原因是因为您正在请求Array对象的单个实例的属性名称。 For a number of reasons, each instance will only have property values that are unique to that instance.出于多种原因,每个实例将仅具有该实例独有的属性值。 Since the values found in Array.prototype are not unique to a specific instance -- which makes sense, not all arrays are going to share the same value for length -- they are shared/inherited for all instances of Array .由于在Array.prototype中找到的值对于特定实例不是唯一的——这是有道理的,并非所有数组都将共享相同的length值——它们为Array所有实例共享/继承。

You could use Object.getOwnPropertyNames你可以使用Object.getOwnPropertyNames

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object. Object.getOwnPropertyNames()方法返回直接在给定对象上找到的所有属性(可枚举或不可枚举Object.getOwnPropertyNames()的数组。

 console.log(Object.getOwnPropertyNames(Array.prototype));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

This method will allow you to pull all the keys and functions from a particular instance of an object (ignoring the unwanted ones):此方法将允许您从对象的特定实例中提取所有键和功能(忽略不需要的):

const ROOT_PROTOTYPE = Object.getPrototypeOf({});

function getAllKeys(object) {
    // do not add the keys of the root prototype object
    if (object === ROOT_PROTOTYPE) {
        return [];
    }

    const names = Object.getOwnPropertyNames(object);

    // remove the default constructor for each prototype
    if (names[0] === 'constructor') {
        names.shift();
    }

    // iterate through all the prototypes of this object, until it gets to the root object.
    return names.concat(getAllKeys(Object.getPrototypeOf(object)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM