我用我认为简单的包装器包装了 jquery,但是任何将函数作为参数的函数似乎都不起作用(该函数从未被调用)。
class $Util {
// jquery wrapper
// use with .apply(this...)
static $() {
if (!this.$wrapper)
throw new ReferenceError('$Util.$: $wrapper not defined in context of this');
var shift = [].shift;
var fn = shift.apply(arguments);
this.$wrapper[fn](arguments);
return this;
}
}
class Foo {
constructor() {
this.$wrapper = $('#foo');
return this;
}
$() {
$Util.$.apply(this, arguments);
return this;
}
}
var f = new Foo().$('appendTo', $('body')); // no problem
f.$('slideUp', function() {
console.log(" i never log !");
});
f.$wrapper.slideUp(function() {
console.log(" but i do !");
});
最佳答案
由于某种原因,将参数更改为 ...arguments 就成功了。
class $Util {
// jquery wrapper
// use with .apply(this...)
static $() {
if (!this.$wrapper)
throw new ReferenceError('$Util.$: $wrapper not defined in context of this');
var shift = [].shift;
var fn = shift.apply(arguments);
this.$wrapper[fn](...arguments);
return this;
}
}
关于javascript - 简单的 jquery 包装器不能完全工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38289022/