简体   繁体   English

哪里可以在jquery-boilerplate中实现window.resize函数?

[英]Where to implement a window.resize function in jquery-boilerplate?

I'm trying to convert one of my plugin written with the jquery plugin pattern with the one provided by jquery-boilerplate . 我正在尝试将使用jquery插件模式编写的插件之一转换为jquery-boilerplate提供的插件。 My plugin relies on a $( window ).resize() function to make it responsive, however when I try to use it on the jquery-boilerplate the web console returns a TypeError when I resize the window browser: 我的插件依靠$( window ).resize()函数使其响应,但是当我尝试在jquery-boilerplate上使用它时,当我调整窗口浏览器的大小时,Web控制台会返回TypeError:

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

// Avoid Plugin.prototype conflicts
$.extend( Plugin.prototype, {
    init: function() {
        this.windowWidth();

        $(window).resize(function(){
            this.windowWidth();
        });
    },

    windowWidth: function() {
        var w = $( window ).width();

        console.log(w);
    }
} );

Web console returns: TypeError: this.windowWidth is not a function . Web控制台返回: TypeError: this.windowWidth is not a function

I tried in this way too: 我也尝试过这种方式:

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

    $(window).resize(function(){
        this.init();
    });
}

// Avoid Plugin.prototype conflicts
$.extend( Plugin.prototype, {
    init: function() {
        this.windowWidth();
    },

    windowWidth: function() {
        var w = $( window ).width();

        console.log(w);
    }
} );

and the web console returns: TypeError: this.init is not a function . 然后Web控制台返回: TypeError: this.init is not a function

Where do I have to put code that have to listen to the jquery resize method according to the jquery-boilerplate ? 我必须在哪里放置必须根据jquery-boilerplate监听jquery resize方法的代码?

I basically made it work in this way: 我基本上以这种方式使其工作:

function Plugin ( element, options ) {
    var element = $( element ),
        cfg = $.extend( true, defaults, options ),

        windowWidth = function() {
            return $( window ).width();
        };

    console.log( windowWidth() );

    $(window).resize(function(){
        console.log( windowWidth() );
    });
}

But this isn't the purpose of the jquery-boilerplate team, so how can I do this while using the jquery-boilerplate plugin pattern? 但这不是jquery-boilerplate团队的目的,那么如何在使用jquery-boilerplate插件模式时做到这一点?

This error has nothing to do with jquery-boilerplate. 此错误与jquery-boilerplate没有关系。 You definitely should learn more about "this" keyword in Javascript before writing any plugins. 在编写任何插件之前,您绝对应该了解更多有关Java中“ this”关键字的信息。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM