0

I created a requireJs module where I already put all the functions:

define(['Scrollbar','module'],function(Scrollbar,module){
     return{
        init:function(){
            //CODE
        },
        manageScroll:function(){
            //CODE
        },
        manageResize:function(){
            //CODE
        },
        transform(){
            //CODE
        }
    }

});

In the init section I defined a handler Jquery on the resize which recall a function on this object

I tryied in this way:

init:function(){
    this.transform();
    jQuery(window).resize(this.manageResize());
},

But it dosn’t work, the only solution I found is this one here below, but it doens’t work on IE.

init:function(){
    this.transform();
    jQuery(window).resize(()=>this.manageResize());
},

Do you have any suggestion or better solution?

Thanks

1 Answer 1

1

You need to pass a callback to jQuery(window).resize(). Something like this:

init:function(){
    this.transform();
    jQuery(window).resize(this.manageResize.bind(this));
},

We need to use .bind(this) otherwise this will have the value undefined inside of the manageResize method. (See Function.prototype.bind for details of how it works.)

In the first attempt, you're passing the return value of this.manageResize(). Unless you are returning a callback, this cannot work.

In your second attempt which does not work in IE, you are using an arrow function, which is simply not supported by any of the IE browsers.

Sign up to request clarification or add additional context in comments.

2 Comments

but now i have error because manageResize call another function this.transform :'(
Sorry. I managed to forget the bind bit. You need to use .bind(this) to make sure that this in bound to the proper value when this.manageResize is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.