【问题标题】:Update Namespace Module Variables from external function从外部函数更新命名空间模块变量
【发布时间】:2015-03-27 02:35:08
【问题描述】:

我会尽量保持这个超级简单,只使用我目前拥有的代码大纲。本质上,我有一个命名空间模块,它是一个自调用函数,它在加载时运行并用变量的值填充变量并返回它们以使其他函数可以访问它们:

moduleA = function() {
    var a = $(window).height(),
        b = $('#imgs').height(),
    // other variables that need updating

    return {
       windowH: a,
       imgH: b
    }
}();

然后在另一个在滚动时调用的函数中访问这些变量:

function scrollSpeed() {
    var start = $('#article-wrap').offset().top,
        end = (start + moduleA.imgH) - moduleA.windowH;

    //use variables in animations
}

$(window).scroll(function() {
    scrollSpeed();
})

上面的问题是当我调整窗口大小时,我需要更新 moduleA 中的变量。最初我在模块中编写了一个函数“update()”并返回它,然后在我的去抖动调整大小函数中调用它,但这不起作用?

moduleA = function() {
    var a = $(window).height(),
        b = $('#imgs').height(),
    // other variables that need updating

    function update() {
       a = $(window).height(),
       b = $('#imgs').height(),
       ...// other variables that are updated
    }

    return {
       windowH: a,
       imgH: b,
       update: update
    }
}();

var resizeFn = debounce(function() {
    moduleA.update();
}, 100);

很明显,我没有在这里包含所有内容,我可以根据要求提供。任何想法我哪里出错了?

【问题讨论】:

    标签: javascript jquery namespaces resize


    【解决方案1】:

    您还需要更新对象属性:

    function update() {
       a = $(window).height(),
       b = $('#imgs').height(),
       ...// other variables that are updated
    
       this.windowH = a;
       this.imgH = b;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      相关资源
      最近更新 更多