I have a file called bbUI.js which contains this bit of JavaScript. Outside of that file, I’d like to be able to call “var x = new iScroll(…)”, but I currently get the error “ReferenceError: Can’t find variable: iScroll”.
(function(){
var iScroll = function (el, options) {
var that = this,
doc = document,
i;
// More code
};
})();
From what I can tell, iScroll is defined within an anonymous function, and is itself anonymous but assigned to the identifier iScroll. If that’s accurate, should I be able to call “var x = new iScroll(…)” in other places in my code?
The
iScrollfunction only exists within the scope of the anonymous function it’s wrapped in. To use it elsewhere, you need to make it global.You can make it global by removing the function it’s wrapped in, or by setting
window.iScroll = iScrollinside the anonymous function.