我有一些这样的功能:
function name_a(){}
function name_b(){}
function name_x(){}
function name_n(){}
我想在javascript中使用正则表达式将所有函数调用 name_
。
我该怎么做?
答案 0 :(得分:1)
只是为了踢,这里有你可以使用的东西。 ES5,因此IE> 9是必需的(虽然可以调整旧浏览器支持)。
/**
* Calls functions of given target object with names matching given regex.
*
* @param {any} targetObject
* @param {RegExp} nameRegex
* @param {...any} functionsArguments
*
* @returns {any[]} The values returned by each function called.
*/
function callFunctionsMatching(targetObject, nameRegex, functionsArguments) {
// make arguments into array, then splice
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
var functionsArgs = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)).splice(2);
return Object.getOwnPropertyNames(targetObject).filter(function (propertyName) {
return typeof targetObject[propertyName] == 'function' && nameRegex.test(propertyName);
}).map(function (functionName) {
return targetObject[functionName].apply(targetObject, functionsArgs);
});
}
全局函数(window
目标对象):
// setup some functions
function a1(a, b, c, d) { console.log('a1->', a, b, c, d); }
function b1(arg) { console.log('b1->', arg); }
window.c1 = console.log
// call every function ending with 1
callFunctionsMatching(window, /1$/, 'stuff', 1, 3, 4);
输出:
a1-> stuff 1 3 4
b1-> stuff
stuff 1 3 4
对象函数(任何对象作为目标):
var person = {
aa: function(x) { console.log('person.aa called with', x); return 'this is the return value of person.aa'; },
aaz: function(x) { console.log('I shouldn have been printed'); }
};
var valuesReturned = callFunctionsMatching(person, /aa$/, 'some argument');
console.log('valuesReturned were', valuesReturned);
输出:
person.aa called with some argument
valuesReturned were ["this is the return value of person.aa"]
来自问题:的示例
function name_a(){ console.log('name_a called'); }
function name_b(){ console.log('name_b called'); }
function name_x(){ console.log('name_x called'); }
function name_n(){ console.log('name_n called'); }
callFunctionsMatching(window, /^name_/, 'args');
输出:
function name_a called
function name_x called
function name_n called
function name_b called
答案 1 :(得分:0)
这不会处理DOM对象,但是如果你有一个普通对象并且只想在一个级别上搜索与某个正则表达式匹配的所有函数,你可以(可选地,按顺序显示每个函数):
function get_func_filter(obj, match) {
var keys = Object.getOwnPropertyNames(obj);
return keys.filter(function(func_name) {
return typeof obj[func_name] === 'function'
&& match.test(func_name);
});
}
var funcs = {
'prop_a': 'value',
'prop_b': 'value',
'name_a': function(){console.log('name_a function called');},
'name_b': function(){console.log('name_b function called');},
'name_c': function(){console.log('name_c function called');},
'not_name_a': function(){console.log('not_name_a function called');},
'not_name_b': function(){console.log('not_name_b function called');},
};
// First, get the function names that match "starting with name_"
get_func_filter(funcs, /^name_/)
// Then get each name
.map(function(func_name) {
// And execute it (the second)
!funcs[func_name] || funcs[func_name]();
}
);