【问题标题】:Jquery - Calling a function within a namespaceJquery - 在命名空间中调用函数
【发布时间】:2012-10-13 15:46:49
【问题描述】:

如何在命名空间中调用常规函数

$(document).ready(function() {
  Timeline.init();
  Timeline.highlight_arrow();
  Timeline.arrows();

});

Timeline.arrows = (function() {
    function display_arrows(pos) {
    }
}());

我正在尝试从 Timeline.arrows 内部调用 dislay_arrows 我尝试过 Timeline.arrows.display_arrows() 它需要是公共方法吗?

【问题讨论】:

    标签: jquery function namespaces


    【解决方案1】:

    你可能需要这样实现,

    Live Demo

    $(document).ready(function() {
        function NS_TimeLine(){
    
            this.init = function(){
             alert("init");   
            }
    
            this.highlight_arrow = function(){
             alert("init");   
            }
    
        }                      
    
    var TimeLine = new NS_TimeLine();
    TimeLine.init();
    
    });
    ​
    

    【讨论】:

      【解决方案2】:

      如果我理解了您的问题,您只需将您的函数从立即执行的函数中传回即可:

      Timeline.arrows = (function() {
          return function display_arrows(pos) {
              //'arrows' implementation here'
          }
      }());
      

      工作演示:jsBin

      这样,(function() { /*...*/}()) 的 /result/ 是 display_arrows 函数,因此将针对 Timeline 上的 arrows 属性进行设置。

      我假设您在使用之前已经声明了一个 Timeline 对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2011-01-11
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        相关资源
        最近更新 更多