Wednesday 15 September 2010

JavaScript: clone a function -


The fastest way to clone a function in JavaScript (with or without its properties)?

Two options coming to mind eval (func.toString ()) and function () {return func.apply (..)} . But I'm worried about the performance of eval and wrapping, will make the stack worse and potentially degrade the display if pre-wrapped or too many have been implemented.

New Function (Args, Body)

Update:

What do I mean

  var funcB = funcA.clone (); // where clone () my extension is funcB.newField = {...};  

Try it out:

  Var x = function () {return1; }; Var t = function (a, b, c) {back a + b + c; }; Function. Protopp Clone = function () {var that = this; Var temp = function temporary () {that.apply return (this, argument); }; For (var key in this) {if (this.hasOwnProperty (key)) {temp [key] = this [key]; }} Return temporary; }; Warning (x === x.clone ()); Warning (x () === x.clone () ()); Warning (T === T. Clone ()); Warning (T (1,1,1) === T. Clone () (1,1,1)); Warning (t.clone () (1,1,1));  

No comments:

Post a Comment