如何在命名空间中调用常规函数
$(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(),它需要是一个公共方法吗?
发布于 2012-10-24 00:30:10
您可能需要像这样实现它,
$(document).ready(function() {
function NS_TimeLine(){
this.init = function(){
alert("init");
}
this.highlight_arrow = function(){
alert("init");
}
}
var TimeLine = new NS_TimeLine();
TimeLine.init();
});
发布于 2012-10-24 00:40:45
如果我已经理解了你的问题,你只需要把你的函数传递回立即执行的函数:
Timeline.arrows = (function() {
return function display_arrows(pos) {
//'arrows' implementation here'
}
}());工作演示:jsBin
这样,您的结果的/ (function() { /*...*/}()) /就是display_arrows函数,因此将根据Timeline上的arrows属性进行设置。
我假设您已经在使用Timeline对象之前声明了它。
https://stackoverflow.com/questions/13035059
复制相似问题