Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Javascript
Creating true copies (of objects) in JS (possible?)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Richard Cornford, post: 4708078"] That possibility didn't occur to me, but it does need to be covered. And thinking about the possibility that the parameter adding function may be called without an argument it also occurred to me that the function (func) itself may not require any parameters, which you have covered here. However, if the function needs no parameters (func.length == 0) shouldn't the value returned at this point be the result of calling that function? As my version allows the initial call to curry to be supplied with all of the required parameters and will execute the function if they are all present I went with executing the function in curry if func.length == 0. Being reminded of the belts and braces was useful, here are my corrections for those (at least the ones I have noticed so far):- function curry(f){ var retVal,obj = this; //global, unless this function //is assigned as a method. arguments.callee.depth = 0; function getArgFunc(args, af){ function argFunc(x){ var retVal; var argsLen = arguments.length; var self = arguments.callee; function getArgs(){ var ar = self.getArgs(); if(argsLen){ar[ar.length] = x;} return ar; } if(self.depth >= f.length){ retVal = f.apply(obj, getArgs()); }else if(!argsLen){ retVal = self; }else{ retVal = getArgFunc(arguments, getArgs); } return retVal; } argFunc.depth = args.callee.depth+1; argFunc.getArgs = af; for(var c = 1;c < args.length;c++){ argFunc = argFunc(args[c]); if((typeof argFunc != 'function')||(!argFunc.getArgs)){ break; } } af = (args = null); return argFunc; } retVal = getArgFunc(arguments, function(){return [];}); return (f.length)?retVal:retVal(); } Richard. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Javascript
Creating true copies (of objects) in JS (possible?)
Top