Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Feedback on: irt.org FAQ Knowledge Base Q1234

irt.org | About | Feedback | 2107 [ previous next ]

Feedback on:
irt.org FAQ Knowledge Base Q1234

Sent by
Rob Roy on December 04, 2000 at 23:08:01:

Worth:
Very worth reading

Comments:
oops - slight correction

A great starting point and it got me going to find a much better solution.

Note (see http://www.eecs.tufts.edu/g/20/notes/js_obj.php3) that the following ways to declare a function are equivalent:

var newFunc = function (arg1, arg2, '{jscript stuff}'); //The method used in your knowledge base

var newFunc = function (arg1, arg2){jscript stuff}; //seems to be much less fussy about contents of the function being cloned.

My new example based on Q1234

function cloneFunction( funcName )
{
// Find first the whole function string. This can even be in another frame:
eval (" var functionString = " + funcName + ".toString();");

// Extract the function body - everything from the first "(" to the end :
var bodyString = functionString.subst( functionString.indexOf("("));

// Add declaration bits and return. Note name of new function will be funcNameCloned:
return "var " + funcName + "Cloned = function " + bodyString;
}

eval(cloneFunction("testwithparm"));
eval(cloneFunction("testnoparm"));





Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 21st December 2007. Maintained by: Martin Webb
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.