【问题标题】:How to fire multiple namespace functions at once?如何一次触发多个命名空间函数?
【发布时间】:2014-10-18 09:27:34
【问题描述】:

是否可以一次调用执行一个命名空间的所有功能?

经验:

var myApp = { 

    e : $('.js-box'),
    addStyle : function(){
    myApp.e.css('height','200');
    },
    warn : function(){
    alert('WOOOOOoooOO');
    }  
};
myApp.addStyle();
myApp.warn();

上面的代码可以正常工作..

我们可以一次调用触发 addStyle 和警告函数吗?

我尝试过/想到的:

var myApp = { 
    workAll : function(){
        e : $('.js-box'),
        addStyle : function(){
        myApp.e.css('height','200');
        },
        warn : function(){
        alert('WOOOOOoooOO');
        }
    }    
};
myApp.workAll();

这不起作用..我该怎么做呢?

现场试用:http://jsfiddle.net/C7JJM/82/

提前谢谢你!

【问题讨论】:

    标签: javascript jquery javascript-namespaces


    【解决方案1】:
    var myApp = { 
         e : $('.js-box'),
    
            addStyle : function(){
            myApp.e.css('height','400');
            },
            warn : function(){
            alert('WOOOOOoooOO');
            }  ,
        addstyleall:function(){this.addStyle();this.warn();}
    
    };
    myApp.addstyleall();
    

    【讨论】:

      【解决方案2】:

      试试这个

          //http://stackoverflow.com/questions/5999998/how-can-i-check-if-a-javascript-variable-is-function-type
          function isFunction(functionToCheck) {
           var getType = {};
           return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
          }
      
          function executeAll(ns){
              if(ns){
              for (property in ns) {
                      if (ns.hasOwnProperty(property)) {
                          var p = ns[property];
                          if (p != null && isFunction(p)) {
                              p();
                          }
                      }
                  }
              }
          }
         var myApp = { 
      
                  e : $('.js-box'),
                  addStyle : function(){
                  myApp.e.css('height','200');
                  },
                  warn : function(){
                  alert('WOOOOOoooOO');
                      }  
          };
          executeAll(myApp)
      

      但要注意传递给函数的参数

      http://jsfiddle.net/s8ng608f/1/

      【讨论】:

        【解决方案3】:

        如果不让每个函数自调用,自动调用所有函数看起来很困难。但是使用自定义调用者,这几乎是可能的..只需在您的第一个正在工作的函数中添加另一个名为 workAll 的函数..

        var myApp = { 
        
                e : $('.js-box'),
                addStyle : function(){
                    console.log("Add style called");
                    myApp.e.css('height','200');
                },
                warn : function(){
                    alert('WOOOOOoooOO!!!');
                },
                runAll : function(){
                    this.addStyle(); //call AddStyle
                    this.warn(); //call Warn
                }
        };
        myApp.runAll();
        

        在这里演示:

        http://jsfiddle.net/C7JJM/84/

        【讨论】:

        • 我需要等待 5 分钟。:)
        猜你喜欢
        • 2016-03-20
        • 1970-01-01
        • 2011-05-25
        • 1970-01-01
        • 2018-12-29
        • 2023-03-02
        • 2012-09-11
        • 2017-10-24
        • 1970-01-01
        相关资源
        最近更新 更多