使用JQuery Boilerplate的Javascript范围问题 [英] Javascript scoping Issue using JQuery Boilerplate

查看:71
本文介绍了使用JQuery Boilerplate的Javascript范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery Boilerplate编写我的第一个JQuery插件-响应式轮播.我正在使用另一个插件(SSM- http://www.simplestatemanager.com/)为每个插件创建状态设备视图.因此,将在移动设备上运行功能A,在平板电脑上运行功能B,在台式机上运行功能C,等等.

I am using the JQuery Boilerplate to write my first JQuery plugin - a responsive carousel. I am using another plugin (SSM - http://www.simplestatemanager.com/) to create states for each device view. So on mobile devices functionA will run, on tablets functionB and on desktops functionC, etc.

对于SSM创建的每个状态,可以添加一个对应的onEnter回调函数,并且我添加了3种合适的函数来在发生这种情况时运行.从下面的代码中可以看到,onEnterDesktop函数在桌面设备上运行.然后,我尝试从这个名为createCarousel()的内部运行其他函数,但收到错误消息未定义createCarousel".

For each state created by SSM a corresponding onEnter callback function can be added and I've added 3 suitable functions to run when this occurs. As you can see from the code below a function onEnterDesktop runs on Desktop devices. I am then attempting to run a further function from within this called createCarousel() but am getting an error "createCarousel is not defined".

我认为这是由于范围问题,并且浏览器无法从onEnterDesktop函数中找到createCarousel.我该如何解决?

I believe this is because of a scoping problem and the browser can't find createCarousel from within the onEnterDesktop function. How can I fix this?

任何帮助都会很棒.这是我的插件的代码:

Any help would be great. Here's the code for my plugin:

;(function ( $, window, document, undefined ) {

// Create the defaults once
var pluginName = "dcResponsiveCarousel",
    defaults = {
        propertyName: "value"
    };

function Plugin( element, options ) {
    this.element = element;
    this.$element = $(element);
    this.options = $.extend( {}, defaults, options) ;
    this._defaults = defaults;
    this._name = pluginName;
    this.init();
}

Plugin.prototype = {

    init: function() {

        ssm.addStates([
            {
                id: 'mobile',
                maxWidth: 767,
                onEnter: this.onEnterMobile
            },            
            {
                id: 'tablet',
                maxWidth: 991,
                minWidth: 768,
                onEnter: this.onEnterTablet
            },
            {
                id: 'desktop',
                minWidth: 992,
                onEnter: this.onEnterDesktop
            }
        ]);
        ssm.ready();
    },

    onEnterMobile: function (){
        console.log("You've entered mobile screen dimension territory");
        var itemsPerPage = 1;
        console.log(itemsPerPage); 
    },

    onEnterTablet: function (){
        console.log("You've entered tablet screen dimension territory");
        var itemsPerPage = 2;
        console.log(itemsPerPage);         
    },

    onEnterDesktop: function (){
         console.log("You've entered desktop screen dimension territory");
         var itemsPerPage = 3;
         createCarousel();
    },

    createCarousel: function (){
        var $carouselItems = this.$element.children(".item"),
            $carouselItemsLength = $carouselItems.length,
            $carouselItemsWidth = 0;

            for (var i = 0; i < $carouselItemsLength; i++) {
                $carouselItemsWidth = $carouselItemsWidth + $carouselItems.eq(i).outerWidth();
            };

            console.log($carouselItemsWidth);
            this.$element
    }
};

$.fn[pluginName] = function ( options ) {
    return this.each(function () {
        if (!$.data(this, "plugin_" + pluginName)) {
            $.data(this, "plugin_" + pluginName,
            new Plugin( this, options ));
        }
    });
};

})( jQuery, window, document );

推荐答案

这是一个对象文字,因此未定义createCarouselPlugin.prototype.createCarousel或在您的作用域内this.createCarousel();

It's an object literal so createCarousel is not defined, Plugin.prototype.createCarousel is, or in the scope you are in, this.createCarousel();

onEnterDesktop: function (){
     console.log("You've entered desktop screen dimension territory");
     var itemsPerPage = 3;
     Plugin.prototype.createCarousel();
},

这篇关于使用JQuery Boilerplate的Javascript范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆