Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Feedback on: irt.org FAQ Knowledge Base Q1234, December 04, 2000 at 23:08:01:

You are here: 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"));




©2018 Martin Webb