这段代码有太多的函数表达式,我想简化它,但我不知道怎么做。
(function(a, b) {
for (a in b = a.prototype)(function(c) {
b[a] = function() {
c.apply(this, arguments);
return this
}
})(b[a])
})(Element);
这段代码有太多的函数表达式,我想简化它,但我不知道怎么做。
(function(a, b) {
for (a in b = a.prototype)(function(c) {
b[a] = function() {
c.apply(this, arguments);
return this
}
})(b[a])
})(Element);
虽然函数语法的使用次数相同,但您可以通过从循环中删除 IIFE 并使其成为命名函数来减少创建的函数数量......
(function(type) {
var proto = type.prototype,
fn;
function createChainable(proto_func) {
return function() {
proto_func.apply(this, arguments);
return this;
};
}
for (fn in proto)
proto[fn] = createChainable(proto[fn]);
})(Element);
我也摆脱了手动混淆。没必要。
外部 IIFE 只是为了防止全局变量。你可以摆脱它,但最终会导致全球污染。
如果这不打扰您,请删除第一行和最后一行,并替换type.prototype
为Element.prototype
.