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

Categories

I have some functions like these:

  • function name_a(){}
  • function name_b(){}
  • function name_x(){}
  • function name_n(){}

I want to call all functions start with name_ with regex in javascript.

How can I do that?

See Question&Answers more detail:os

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

1 Answer

Just for the kicks, here's something you can use. ES5, so IE>9 is required (could be tweaked for older browsers support, though).

/**
 * 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);
    });
}

Usage (demo JSBin here)

Global functions (window target object):

// 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);

Output:

a1-> stuff 1 3 4
b1-> stuff
stuff 1 3 4

Objects functions (any object as target):

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);

Output:

person.aa called with some argument
valuesReturned were ["this is the return value of person.aa"]

Example from the question:

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');

Output:

function name_a called
function name_x called
function name_n called
function name_b called

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...