在JavaScript中克隆函数(带或不带其属性)最快的方法是什么?
脑海中浮现出两个选项:eval(func.toString())
和function() { return func.apply(..) }
。但我担心eval和包装的性能会使堆栈变得更糟,如果大量应用或应用到已经包装的地方,可能会降低性能。
new Function(args, body)
看起来不错,但在JS中没有JS解析器的情况下,我如何才能可靠地将现有函数拆分成args和body呢?
提前谢谢。
更新:我的意思是能够做到
var funcB = funcA.clone(); // where clone() is my extension
funcB.newField = {...}; // without affecting funcA
发布于 2009-12-03 00:00:37
试试这个:
var x = function() {
return 1;
};
var t = function(a,b,c) {
return a+b+c;
};
Function.prototype.clone = function() {
var that = this;
var temp = function temporary() { return that.apply(this, arguments); };
for(var key in this) {
if (this.hasOwnProperty(key)) {
temp[key] = this[key];
}
}
return temp;
};
alert(x === x.clone());
alert(x() === x.clone()());
alert(t === t.clone());
alert(t(1,1,1) === t.clone()(1,1,1));
alert(t.clone()(1,1,1));
发布于 2011-07-21 15:27:46
以下是更新后的答案
var newFunc = oldFunc.bind({}); //clones the function with '{}' acting as its new 'this' parameter
但是,.bind
是JavaScript (带有compatibility workaround from MDN)的现代( >=iE9 )特性
备注
即使在新的函数this
调用中,新的函数
bind()
上给出的参数卡住。归功于@Kevinfunction oldFunc() {
console.log(this.msg);
}
var newFunc = oldFunc.bind({ msg: "You shall not pass!" }); // this object is binded
newFunc.apply({ msg: "hello world" }); //logs "You shall not pass!" instead
instanceof
将newFunc
/oldFunc
视为相同。归功于@Christopher(new newFunc()) instanceof oldFunc; //gives true
(new oldFunc()) instanceof newFunc; //gives true as well
newFunc == oldFunc; //gives false however
发布于 2012-06-27 23:37:57
这是Jared答案的一个稍微更好的版本。克隆的次数越多,这个函数就不会嵌套得越深。它总是调用原始的。
Function.prototype.clone = function() {
var cloneObj = this;
if(this.__isClone) {
cloneObj = this.__clonedFrom;
}
var temp = function() { return cloneObj.apply(this, arguments); };
for(var key in this) {
temp[key] = this[key];
}
temp.__isClone = true;
temp.__clonedFrom = cloneObj;
return temp;
};
此外,作为对pico.creator给出的更新答案的回应,值得注意的是,在Javascript 1.8.5中添加的bind()
函数与Jared的答案具有相同的问题-它将继续嵌套,导致每次使用时函数的速度越来越慢。
https://stackoverflow.com/questions/1833588
复制相似问题