简体   繁体   English

在Javascript中,如果有一个对象具有很多作为函数的属性,那么如何将它们转换为(函数名称的)字符串数组?

[英]In Javascript, if there is an object with a lot of properties that are functions, how do you convert them to array of strings (of the function names)?

In Javascript, if an object has lots of properties that are functions: 在Javascript中,如果对象具有许多作为函数的属性:

var obj = { foo: function() { ... },
            bar: function() { ... },
              ...
          }

then how can you get an array of names of those functions? 那么如何获得这些函数的名称数组? That is, an array 也就是说,一个数组

["foo", "bar", ... ]

thanks. 谢谢。

var names = [];
for( var k in obj ) {
   if(obj.hasOwnProperty(k) && typeof obj[k] == 'function') {
      names.push(k);
   }
}
var functions = [];
for (var prop in obj) {
    if ((typeof obj[prop]) == 'function') {
        // it's a function
        functions.push(prop);
    }
}

Edit: I've slightly misread the question, you want to extract the names of only the properties that are function objects: 编辑:我稍微误解的问题,要提取只有那些函数对象的属性的名称:

function methods(obj) {
  var result = [];
  for (var prop in obj) {
    if (obj.hasOwnProperty(prop) && typeof obj[prop] == 'function') {
      result.push(prop);
    }
  }
  return result;
}

var obj = {
  foo: function() { },
  bar: function() { },
};

methods(obj); // ["foo", "bar"]

I'm using the hasOwnProperty method, to ensure that the enumerated properties in fact exist physically in the object. 我正在使用hasOwnProperty方法,以确保枚举的属性实际上实际上存在于对象中。

Note that this approach and all other answers have a small problem IE. 请注意,此方法和所有其他答案都有一个小的问题IE。

The JScript's DontEnum Bug , custom properties that shadow non-enumerable properties ( DontEnum ) higher in the prototype chain, are not enumerated using the for-in statement, for example : JScript的DontEnum Bug是在原型链中DontEnum不可枚举属性( DontEnum )的自定义属性,不会使用for-in语句枚举,例如:

var foo = {
  constructor : function() { return 0; },
  toString : function() { return "1"; },
  valueOf : function() { return 2; }
  toLocaleString : function() { return "3"; }
};

for (var propName in foo ) { alert(propName); }

The object foo clearly has defined four own properties, but those properties exist in Object.prototype marked as DontEnum , if you try to enumerate the properties of that object with the for-in statement in IE, it won't find any. 对象foo显然定义了四个自己的属性,但是这些属性存在于标为DontEnum Object.prototype ,如果尝试使用IE中的for-in语句枚举该对象的属性,它将找不到任何属性。

This bug is present on all IE versions, and has been recently fixed in IE9 Platform Preview. 此错误存在于所有IE版本中,并且最近已在IE9 Platform Preview中修复。

To complete other answers: you can also use instanceof : 要完成其他答案:您还可以使用instanceof

var obj = { foo: function() { ... },
            bar: function() { ... },
              ...
          },
    fnArr = [];

for (var label in obj){
  if (obj[label] instanceof Function){
     fnArr.push(label)
  }
}

With ES5 : 使用ES5

var obj = {
    foo: function() {},
    bar: function() {},
    baz: true
};

function getMethods(object) {
    return Object.keys(object).filter(function(key) {
        return typeof object[key] == 'function';
    });
}

getMethods(obj); // [foo, bar]

Object.keys(<object>) returns the names of all enumerable properties of an object as an array, of which the non-functions are filtered out. Object.keys(<object>)以数组形式返回Object.keys(<object>)的所有可枚举属性的名称,其中的所有非函数均被滤除。

Example - works on Chrome release and nightly builds of Webkit and Tracemonkey (Firefox) . 示例 -适用于Chrome版本以及WebkitTracemonkey(Firefox)的夜间构建。

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

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