Jquery - 在命名空间内调用函数

时间:2012-10-23 16:25:32

标签: jquery function namespaces

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

$(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()它需要是一个公共方法吗?

2 个答案:

答案 0 :(得分:0)

您可能需要像这样实现它,

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

});
​

答案 1 :(得分:0)

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

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

工作演示:jsBin

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

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

相关问题