JavaScript函数中的全局变量

时间:2016-03-10 14:56:28

标签: javascript jquery

我有以下代码:

function timeLine (wrapper,slider,boxwidth,labels,labelWdth) {
    this.wrapper = wrapper;
    this.slider = slider;
    this.boxwidth = boxwidth;
    this.labels = labels;
    this.labelWdth = labelWdth;

    this.setupBlocks = function(){          
        $(this.wrapper+' .blocks').each(function(){
            // Get longest block for some reason this causes us problems!
            newWidth = $(this).data('cover') * this.boxwidth;

            $(this).width(newWidth+'px');
            $(this).css('margin-left',$(this).data('left') * this.boxwidth+'px');
        })  
    }

我发现的问题是this.boxwidth函数中的setupBlocks变量正在返回NULL

如果我在功能级别定义了这些属性,this.variable是否仍然可用?

我想jQuery each会覆盖它,那么如何将this.列表设置为完全全局?

我认为你必须以这种格式为JS类提供这个(根据我使用的http://www.phpied.com/3-ways-to-define-a-javascript-class/,因为我通常不用JS做类)。

0 个答案:

没有答案
相关问题