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

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

问题描述

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

  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() 获取属于对象的所有属性,无论是否可枚举。例如:

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

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

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






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



来自 ECMA-262 Edition 3


全局对象

有一个唯一的全局
对象(15.1),它是在
控件进入任何执行上下文之前创建的。
最初,全局对象具有
以下属性:



•内置
对象,如Math,String,Date,
parseInt等这些属性具有{
DontEnum}


•其他主机定义的
属性。这可能包括
属性,其值为全局
对象本身;例如,在
HTML文档对象模型中,全局对象的窗口
属性是
全局对象本身。



由于控制
进入执行上下文,并且执行
ECMAScript代码,
附加属性可能会添加到
可以更改全局对象和初始
属性。


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




更新:一位SO用户CMS带来了关于 {DontEnum} 的IE漏洞引起了我的注意。


除了检查DontEnum属性之外,[Microsoft] JScript将跳过对象原型链中具有属性DontEnum的同名属性的任何对象中的任何属性。


简而言之,在命名对象属性时要小心。如果存在具有相同名称的内置原型属性或方法,那么当在循环中使用 for ...时,IE将跳过它。


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

 alert(show_all_methods(Math));

This should print:

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

解决方案

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 ]

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 ]


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.

From ECMA-262 Edition 3:

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:

• 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.

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.

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.


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

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.

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天全站免登陆