如何显示一个对象的所有方法? [英] How to display all methods of an object?

查看:26
本文介绍了如何显示一个对象的所有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何列出对象的所有可用方法,例如:

I want to know how to list all methods available for an object like for example:

 alert(show_all_methods(Math));

这应该打印:

abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random,round, sin, sqrt, tan, …

推荐答案

您可以使用 Object.getOwnPropertyNames() 获取属于一个对象的所有属性,无论是否可枚举.例如:

You can use Object.getOwnPropertyNames() to get all properties that belong to an object, whether enumerable or not. For example:

console.log(Object.getOwnPropertyNames(Math));
//-> ["E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", ...etc ]

然后您可以使用 filter() 只获取方法:

You can then use filter() to obtain only the methods:

console.log(Object.getOwnPropertyNames(Math).filter(function (p) {
    return typeof Math[p] === 'function';
}));
//-> ["random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", ...etc ]

<小时>

在 ES3 浏览器(IE 8 及更低版本)中,内置对象的属性不可枚举.像 windowdocument 这样的对象不是内置的,它们是由浏览器定义的,很可能是设计可枚举的.


In ES3 browsers (IE 8 and lower), the properties of built-in objects aren't enumerable. Objects like window and document aren't built-in, they're defined by the browser and most likely enumerable by design.

来自 ECMA-262 版本 3:

全局对象
有一个独特的全球对象(15.1),之前创建的控制进入任何执行上下文.最初,全局对象具有以下属性:

Global Object
There is a unique global object (15.1), which is created before control enters any execution context. Initially the global object has the following properties:

• 内置对象,例如 Math、String、Date、parseInt 等.这些都有属性{DontEnum }.
• 额外的主机定义特性.这可能包括一个其值为全局的属性对象本身;例如,在HTML 文档对象模型窗口全局对象的属性是全局对象本身.

• Built-in objects such as Math, String, Date, parseInt, etc. These have attributes { DontEnum }.
• Additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.

作为对照进入执行上下文,并作为ECMAScript 代码被执行,可以添加额外的属性全局对象和初始属性可能会改变.

As control enters execution contexts, and as ECMAScript code is executed, additional properties may be added to the global object and the initial properties may be changed.

我应该指出,这意味着这些对象不是 Global 对象的可枚举属性.如果您查看规范文档的其余部分,您将看到这些对象的大多数内置属性和方法都设置了 { DontEnum } 属性.

I should point out that this means those objects aren't enumerable properties of the Global object. If you look through the rest of the specification document, you will see most of the built-in properties and methods of these objects have the { DontEnum } attribute set on them.

更新:一位 SO 用户,CMS,带来了一个 关于 { DontEnum } 的 IE 错误 引起我的注意.

Update: a fellow SO user, CMS, brought an IE bug regarding { DontEnum } to my attention.

[Microsoft] JScript 将跳过任何对象中的任何属性,而不是检查 DontEnum 属性,如果该对象的原型链中有一个具有 DontEnum 属性的同名属性.

Instead of checking the DontEnum attribute, [Microsoft] JScript will skip over any property in any object where there is a same-named property in the object's prototype chain that has the attribute DontEnum.

简而言之,在命名对象属性时要小心.如果存在同名的内置原型属性或方法,则 IE 将在使用 for...in 循环时跳过它.

In short, beware when naming your object properties. If there is a built-in prototype property or method with the same name then IE will skip over it when using a for...in loop.

这篇关于如何显示一个对象的所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆