我正在构建一个插件,为了打开/关闭某些行为,我正在使用 matchMedia 函数。但是在回调函数中,我需要传递一个参数来保留对此的正确引用。但是,当我尝试将参数传递给回调函数时,它说我的整个 addListener 回调函数未定义。当我尝试将其绑定到回调函数时,这同样适用。
TypeError: this.mediaQueryCheck(...) is undefined
Run Code Online (Sandbox Code Playgroud)
关于 addListener ,我一定缺少一些非常明显的东西,所以现在我只包含我的代码的非功能示例:
MyPlugin.prototype = {
version : '0.0.1',
mediaQuery : window.matchMedia(defaults.breakpoint),
mediaQueryCheck : function(mql){
if(mql.matches === true){ // if our mediaQuery matches
this.evalScrollPosition();
if(this.isLaunched === false){
// attach scroll handlers
$(window).on('scroll resize', this.evalScrollPosition.bind(this));
this.isLaunched = true;
}
}
else if(mql.matches === false){ // if the mediaQuery isn't active atm
if(this.isLaunched === true){
// remove handlers
$(window).off('scroll resize', this.evalScrollPosition.bind(this));
this.isLaunched = false;
}
this.fixedStatus = '';
this.unstyleContainer(); // remove positioning set …
Run Code Online (Sandbox Code Playgroud)