我想知道如何列出一个对象可用的所有方法,例如:
alert(show_all_methods(Math));
这应该打印:
abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random,round, sin, sqrt, tan, …
我想知道如何列出一个对象可用的所有方法,例如:
alert(show_all_methods(Math));
这应该打印:
abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random,round, sin, sqrt, tan, …
当前回答
这在ES3中是不可能的,因为属性有一个内部的DontEnum属性,这阻止了我们枚举这些属性。另一方面,ES5提供了用于控制属性枚举功能的属性描述符,这样用户定义的属性和本机属性可以使用相同的接口并享有相同的功能,这包括能够以编程方式查看不可枚举的属性。
getOwnPropertyNames函数可用于枚举传入对象的所有属性,包括那些不可枚举的属性。然后可以使用一种简单的检查类型来过滤非函数。不幸的是,Chrome是目前唯一可以运行的浏览器。
function getAllMethods(object) {
return Object.getOwnPropertyNames(object).filter(function(property) {
return typeof object[property] == 'function';
});
}
console.log(getAllMethods(Math));
日志(“因为”、“战俘”、“日志”,“晒”,“√”,“装天花板”、“最佳”、“abs”、“max”、“实验”、“量化”,“随机”,“圆”,“地板”,“治疗”,“世界”,“分”,“罪”)在任何特定的顺序。
其他回答
这里的其他答案适用于Math,这是一个静态对象。但它们不适用于对象的实例,比如日期。我发现以下方法是有效的:
function getMethods(o) {
return Object.getOwnPropertyNames(Object.getPrototypeOf(o))
.filter(m => 'function' === typeof o[m])
}
//example: getMethods(new Date()): [ 'getFullYear', 'setMonth', ... ]
https://jsfiddle.net/3xrsead0/
这并不适用于原始问题(数学),所以根据你的需要选择你的解决方案。我在这里张贴这个,因为谷歌发送给我这个问题,但我想知道如何为对象的实例这样做。
与ES6类和继承很好地工作的方法
这可能是大多数像我这样的ES6新手在寻找“如何列出对象方法”时的意思。
本文已改编自:https://stackoverflow.com/a/47714550/895245
// Define getMethods.
const isGetter = (x, name) => (Object.getOwnPropertyDescriptor(x, name) || {}).get
const isFunction = (x, name) => typeof x[name] === "function";
const deepFunctions = x =>
x && x !== Object.prototype &&
Object.getOwnPropertyNames(x)
.filter(name => isGetter(x, name) || isFunction(x, name))
.concat(deepFunctions(Object.getPrototypeOf(x)) || []);
const distinctDeepFunctions = x => Array.from(new Set(deepFunctions(x)));
const getMethods = (obj) => distinctDeepFunctions(obj).filter(
name => name !== "constructor" && !~name.indexOf("__"));
// Example usage.
class BaseClass {
override() { }
baseMethod() { }
}
class DerivedClass extends BaseClass {
override() { }
get myGetter() { }
static myStatic() { }
}
const obj = new DerivedClass();
const methods = getMethods(obj)
methods.sort()
const assert = require('assert')
assert(methods[0] === 'baseMethod')
assert(methods[1] === 'myGetter')
assert(methods[2] === 'override')
console.log(getMethods(Math))
注意它还包括基类的方法,因为大多数用户都想知道他们可以在对象上调用哪些方法。
它似乎也与Math一起工作,它输出:
[
'abs', 'acos', 'acosh', 'asin',
'asinh', 'atan', 'atanh', 'atan2',
'ceil', 'cbrt', 'expm1', 'clz32',
'cos', 'cosh', 'exp', 'floor',
'fround', 'hypot', 'imul', 'log',
'log1p', 'log2', 'log10', 'max',
'min', 'pow', 'random', 'round',
'sign', 'sin', 'sinh', 'sqrt',
'tan', 'tanh', 'trunc'
]
在Node.js 14.17.0上测试。
这在ES3中是不可能的,因为属性有一个内部的DontEnum属性,这阻止了我们枚举这些属性。另一方面,ES5提供了用于控制属性枚举功能的属性描述符,这样用户定义的属性和本机属性可以使用相同的接口并享有相同的功能,这包括能够以编程方式查看不可枚举的属性。
getOwnPropertyNames函数可用于枚举传入对象的所有属性,包括那些不可枚举的属性。然后可以使用一种简单的检查类型来过滤非函数。不幸的是,Chrome是目前唯一可以运行的浏览器。
function getAllMethods(object) {
return Object.getOwnPropertyNames(object).filter(function(property) {
return typeof object[property] == 'function';
});
}
console.log(getAllMethods(Math));
日志(“因为”、“战俘”、“日志”,“晒”,“√”,“装天花板”、“最佳”、“abs”、“max”、“实验”、“量化”,“随机”,“圆”,“地板”,“治疗”,“世界”,“分”,“罪”)在任何特定的顺序。
您可以使用object . getownpropertynames()来获取属于一个对象的所有属性,无论是否可枚举。例如:
console.log(Object.getOwnPropertyNames(Math));
//-> ["E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", ...etc ]
然后你可以使用filter()只获取方法:
console.log(Object.getOwnPropertyNames(Math).filter(function (p) {
return typeof Math[p] === 'function';
}));
//-> ["random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", ...etc ]
在ES3浏览器(ie8及以下版本)中,内置对象的属性是不可枚举的。像window和document这样的对象不是内置的,它们是由浏览器定义的,而且很可能是通过设计来枚举的。
来自ECMA-262版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.
我应该指出,这意味着这些对象不是全局对象的可枚举属性。如果查看规范文档的其余部分,您将看到这些对象的大多数内置属性和方法都设置了{DontEnum}属性。
更新:一个SO用户,CMS,给我带来了一个关于{DontEnum}的IE bug。
而不是检查DontEnum属性,[Microsoft] JScript将跳过任何对象中的任何属性,在对象的原型链中有一个具有DontEnum属性的同名属性。
简而言之,在命名对象属性时要小心。如果有一个内置的原型属性或方法具有相同的名称,那么IE将在使用for…在循环。
如果你想要包括继承方法在内的所有方法:
函数getMethods(obj) { Const方法= []; {做 for (Object.getOwnPropertyNames(obj)的const道具){ if (obj[prop] instanceof Function) method .push(prop); } obj = Object.getPrototypeOf(obj); } while (obj !== null) 回归方法; } console.log (getMethods(数学));