首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript:克隆函数

JavaScript:克隆函数
EN

Stack Overflow用户
提问于 2009-12-02 23:22:19
回答 15查看 111.9K关注 0票数 136

在JavaScript中克隆函数(带或不带其属性)最快的方法是什么?

脑海中浮现出两个选项:eval(func.toString())function() { return func.apply(..) }。但我担心eval和包装的性能会使堆栈变得更糟,如果大量应用或应用到已经包装的地方,可能会降低性能。

new Function(args, body)看起来不错,但在JS中没有JS解析器的情况下,我如何才能可靠地将现有函数拆分成args和body呢?

提前谢谢。

更新:我的意思是能够做到

代码语言:javascript
复制
var funcB = funcA.clone(); // where clone() is my extension
funcB.newField = {...};    // without affecting funcA
EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2009-12-03 00:00:37

试试这个:

代码语言:javascript
复制
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));
票数 58
EN

Stack Overflow用户

发布于 2011-07-21 15:27:46

以下是更新后的答案

代码语言:javascript
复制
var newFunc = oldFunc.bind({}); //clones the function with '{}' acting as its new 'this' parameter

但是,.bind是JavaScript (带有compatibility workaround from MDN)的现代( >=iE9 )特性

备注

  1. It not clone function object附加的代码属性, prototype属性。归功于@jchook

即使在新的函数this调用中,新的函数

  1. 变量也会使用bind()上给出的参数卡住。归功于@Kevin

代码语言:javascript
复制
function 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

  1. 绑定的函数对象,instanceofnewFunc/oldFunc视为相同。归功于@Christopher

代码语言:javascript
复制
(new newFunc()) instanceof oldFunc; //gives true
(new oldFunc()) instanceof newFunc; //gives true as well
newFunc == oldFunc; //gives false however
票数 131
EN

Stack Overflow用户

发布于 2012-06-27 23:37:57

这是Jared答案的一个稍微更好的版本。克隆的次数越多,这个函数就不会嵌套得越深。它总是调用原始的。

代码语言:javascript
复制
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的答案具有相同的问题-它将继续嵌套,导致每次使用时函数的速度越来越慢。

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1833588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档