Skip to main content
Other problems
Zsombor Deak avatar
Written by Zsombor Deak
Updated over 4 months ago

Cannot access variables and methods form Mobiscroll events with this keyword

A function's this keyword behaves a little differently in JavaScript compared to other languages. The problem usually occurs with those frameworks where the this keyword is used to access the app's methods and properties.

If the mobiscroll events are defined the following way:

onBeforeShow: function (event, inst) {
// this.yourPropertyName won't point to the actual property
}

In Mobiscroll event handlers that are defined like above, this keyword points to the element where the component was initialized.
โ€‹
โ€‹Solution: The easiest way to solve this problem is to use ES6 arrow functions where this retains the value of the enclosing lexical context's this:

onBeforeShow: (event, inst) => {
// this.yourPropertyName will point to your actual property
}

If you couldn't solve the error which you are experiencing get in touch at support@mobiscroll.com.

Did this answer your question?