Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

javascript - 如何显示对象的所有方法?(How to display all methods of an object?)

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, …   ask by GeekTantra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use Object.getOwnPropertyNames() to get all properties that belong to an object, whether enumerable or not.(您可以使用Object.getOwnPropertyNames()来获取属于对象的所有属性,无论是否可枚举。)

For example:(例如:) console.log(Object.getOwnPropertyNames(Math)); //-> ["E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", ...etc ] You can then use filter() to obtain only the methods:(然后,您可以使用filter()仅获取方法:) 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.(在ES3浏览器(IE 8及更低版本)中,内置对象的属性不可枚举。) Objects like window and document aren't built-in, they're defined by the browser and most likely enumerable by design.(windowdocument类的对象不是内置的,它们由浏览器定义,很可能是设计可枚举的。) From ECMA-262 Edition 3 :(来自ECMA-262 Edition 3 :) Global Object(全球对象)
There is a unique global object (15.1), which is created before control enters any execution context.(有一个唯一的全局对象(15.1),它在控件进入任何执行上下文之前创建。) Initially the global object has the following properties:(最初,全局对象具有以下属性:) ? Built-in objects such as Math, String, Date, parseInt, etc. These have attributes { DontEnum } .(?内置对象,如Math,String,Date,parseInt等。 这些对象具有{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.(例如,在HTML文档对象模型中,全局对象的window属性是全局对象本身。) 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.(当控件进入执行上下文时,并且当执行ECMAScript代码时,可以向全局对象添加附加属性,并且可以更改初始属性。) I should point out that this means those objects aren't enumerable properties of the Global object.(我应该指出,这意味着这些对象不是Global对象的可枚举属性。) 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.(如果查看规范文档的其余部分,您将看到这些对象的大多数内置属性和方法都设置了{ DontEnum }属性。) Update: a fellow SO user, CMS, brought an IE bug regarding { DontEnum } to my attention.(更新:一个SO用户,CMS,引起了关于{ DontEnum }IE错误 。) 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.(而不是检查DontEnum属性,[Microsoft] JScript将跳过对象原型链中具有属性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.(如果存在具有相同名称的内置原型属性或方法,则IE将在使用for...in循环时跳过它。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...