克隆javascript函数

克隆javascript函数,javascript,function,clone,Javascript,Function,Clone,我有一个函数构造函数,它用一个函数创建一个对象,比如说,do\u it: function do_it_maker() { this.do_it = function() { /* do something */ }; this.other_do = function() { /* do anything else */ }; } 我想为do_it_maker对象创建一个包装器,通过寄生继承来丰富do_it行为: function do_it_maker_wrapper()

我有一个函数构造函数,它用一个函数创建一个对象,比如说,
do\u it

function do_it_maker() {
     this.do_it = function() { /* do something */ };
     this.other_do = function() { /* do anything else */ };
}
我想为
do_it_maker
对象创建一个包装器,通过寄生继承来丰富
do_it
行为:

function do_it_maker_wrapper() {
     var do_it_obj = new do_it_maker();

     do_it_obj.do_it_original = do_it_obj.do_it;

     do_it_obj.do_it = function() {
         do_it_obj.do_it_original();
         /* smth_else */
     }

     return do_it_obj;
}

var do_it_obj = new do_it_maker_wrapper();
do_it_obj.do_it(); // Reimplemented version.
do_it_obj.other_do(); // Untouched.
我的问题是,
do\u it\u obj.do\u it()
调用是否会导致递归调用?或者对原始
函数的引用是否仍然有效

我在StackOverflow中找到了相关的问题,但我读到了关于是否必须克隆该方法的矛盾答案


那么,会发生什么呢?如果在更改
后引用变为递归引用,那么使用克隆该方法所需的代码量最少且最容易理解的方法是什么?我是C++,不喜欢复制粘贴代码的家伙,有些JavaScript特性看起来仍然晦涩难懂(我不是笨拙的,但是你可以做的有点难)。
function do_it_maker_wrapper() {
    var do_it_obj = new do_it_maker();

    // in this line you save/keep the reference to 'do_it' original function on a 
    // completely new reference.
    do_it_obj.do_it_original = do_it_obj.do_it;

    // this line replaces the reference to original 'do_it' with a reference to a
    // new function that we create here
    do_it_obj.do_it = function() {

         // this still references the original 'do_it' function so that is called
         do_it_obj.do_it_original();
         / * smth_else */
    }
}.

var do_it_obj = new do_it_maker_wrapper();

// this property references the function that we created as a replacement for the original 'do_it'
do_it_obj.do_it();
编辑:
随着ES6在几乎所有地方都可用,现在您不需要再像那样操之过急,只需像在任何其他OO语言中一样使用“class”和“extend”。实际上,它甚至可以帮助像v8这样的运行时更好地优化您的代码,并且它的速度与创建带有花括号“{}”的简单哈希一样快。

它将调用原始函数,不会发生递归

function do_it_maker_wrapper() {
    var do_it_obj = new do_it_maker();

    // in this line you save/keep the reference to 'do_it' original function on a 
    // completely new reference.
    do_it_obj.do_it_original = do_it_obj.do_it;

    // this line replaces the reference to original 'do_it' with a reference to a
    // new function that we create here
    do_it_obj.do_it = function() {

         // this still references the original 'do_it' function so that is called
         do_it_obj.do_it_original();
         / * smth_else */
    }
}.

var do_it_obj = new do_it_maker_wrapper();

// this property references the function that we created as a replacement for the original 'do_it'
do_it_obj.do_it();
编辑:
随着ES6在几乎所有地方都可用,现在您不需要再像那样操之过急,只需像在任何其他OO语言中一样使用“class”和“extend”。实际上,它甚至可以帮助像v8这样的运行时更好地优化您的代码,并且它的速度与创建一个带有小括号“{}”的简单哈希一样快。

什么是
self
?这似乎不起作用。当您的工厂函数返回对象时,您不应该使用
new
操作符。您刚刚试过吗?确定调用是递归(和堆栈溢出)还是按预期调用父方法应该非常简单。什么是
self
?这似乎不起作用。当您的工厂函数返回对象时,您不应该使用
new
操作符。您刚刚试过吗?确定调用是递归(和stackoverflows)还是按预期调用父方法应该非常简单。