最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript Dynamic Function Call with Name - Stack Overflow

programmeradmin1浏览0评论

So I have the following pseudo Javascript code:

var Class = (function(window, document, $) {
    function meth_1()
    {
        //some code
    }

    function meth_2()
    {
        //some code
    }

    function meth_3()
    {
        //some code
    }

    function meth_4()
    {
        //some code to call other three functions dynamically
    }

    Class = {
        meth_1: meth_1,
        meth_2: meth_2,
        meth_3: meth_3,
        meth_4: meth_4
    };
    return Class;

})(window, document, jQuery);

In the meth_4 function, I want to call the other 3 functions dynamically by passing the function name as a string. How can I do this?!

In this related StackOverflow question, the answer provides a solution to how this could be done in window scope i.e. window[function_name](). However, I'd like to know how I can do it in my particular circumstance.

Thanks.

EDIT

The answer I selected works ok. You could also do the following:

var Class = (function(window, document, $) {
    var meth_func = {
        meth_1: function(){/**your code**/},
        meth_2: function(){/**your code**/},
        meth_3: function(){/**your code**/}            
    }

    function meth_4(func_name)
    {
        meth_func[func_name]();
    }

    Class = {
        meth_4: meth_4
    };
    return Class;

})(window, document, jQuery);

This would probably work better if you wanted to make private those three functions being called dynamically.

So I have the following pseudo Javascript code:

var Class = (function(window, document, $) {
    function meth_1()
    {
        //some code
    }

    function meth_2()
    {
        //some code
    }

    function meth_3()
    {
        //some code
    }

    function meth_4()
    {
        //some code to call other three functions dynamically
    }

    Class = {
        meth_1: meth_1,
        meth_2: meth_2,
        meth_3: meth_3,
        meth_4: meth_4
    };
    return Class;

})(window, document, jQuery);

In the meth_4 function, I want to call the other 3 functions dynamically by passing the function name as a string. How can I do this?!

In this related StackOverflow question, the answer provides a solution to how this could be done in window scope i.e. window[function_name](). However, I'd like to know how I can do it in my particular circumstance.

Thanks.

EDIT

The answer I selected works ok. You could also do the following:

var Class = (function(window, document, $) {
    var meth_func = {
        meth_1: function(){/**your code**/},
        meth_2: function(){/**your code**/},
        meth_3: function(){/**your code**/}            
    }

    function meth_4(func_name)
    {
        meth_func[func_name]();
    }

    Class = {
        meth_4: meth_4
    };
    return Class;

})(window, document, jQuery);

This would probably work better if you wanted to make private those three functions being called dynamically.

Share edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Mar 4, 2013 at 13:42 ObiHillObiHill 11.9k24 gold badges92 silver badges142 bronze badges 4
  • 1 Why don't you just create an array of functions? – Lee Taylor Commented Mar 4, 2013 at 13:43
  • 1 @LeeTaylor How?! Can you post an answer on that?! – ObiHill Commented Mar 4, 2013 at 13:49
  • to make it clear, you are looking for an Execute function, so that meth4 body could be : Execute("Class.meth_1"); Execute("class.meth_2");... ? – jbl Commented Mar 4, 2013 at 13:53
  • @jbl I'm not sure I understand you. I want to call the first three functions in a dynamic way from the fourth using only the function name. Just like I can use window[function_name]() is there an alternative for when I want to do the same thing within meth_4. – ObiHill Commented Mar 4, 2013 at 14:12
Add a ment  | 

3 Answers 3

Reset to default 2

Since you want to call functions using bracket notation in object scope, not window scope, you can use this instead of window:

function meth_4()
{
    this["meth_1"]();
    this["meth_2"]();
    this["meth_3"]();
}

I don't know why you would want that (also 'Class' is not good as a variable name), but could be done like this:

var Class = (function (window /*, ... */) {
  return {
    func1: function () {console.log('1')}
  , func2: function () {console.log('2')}
  , func3: function () {console.log('3')}
  , func4: function (funcName) {
      console.log('4+')
      this[funcName]()
    }
  }
})(window)

Class.func4('func3')

Here's an implementation that can do what you want. If you pass a string it will simply look up the appropriate function and execute it. However since functions in JS are indeeed objects them self you can pass those around and to show that I've extended the functionality a bit pared to what you ask for. If you pass a function (actual anything but a string) it will (try to) invoke it

meth_4 = function(fun){
   if(typeof fun === 'string'){
       this[fun](args.slice(1));
   } else { //assuming it's a function
       if(arguments.length == 1)
          fun.call(this,[])
       } else {
          fun.apply(this,arguments.slice(1))  
       }
   }
}

JavaScript objects are, or at can be treated as, a key/value pair collection. Any function or property can be access in two semantically equivalent ways

obj.key or obj["key"] in certain cases only the latter can be used. E.g. if the property is called property nameyou can't do obj.property name but can do obj["property name"]

发布评论

评论列表(0)

  1. 暂无评论