When testing for a partial evaluation function:
function partialEval(fn)
{
var sliceMethod = Array.prototype.slice;
if(arguments.length > 1)
{
var aps = sliceMethod.call(arguments, 1);
}
return function () {
return fn.apply(this,aps.concat( sliceMethod.call(arguments) ));
};
}
var x= function add(a,b,c,d){
console.debug(a + " - " + b + " - " + c + " - " + d);
return a+b+c+d;
}
var pa = partialEval(add, 1,2); // Query here
var zz = pa(3,4);
console.debug(zz);
What is the difference between calling partialEval(add,1,2) and partialEval (x,1,2)? I understand that x is a function literal here and using x gives the correct results. But when I use add as a function name sent to the partialEval method the output is coming as 3. Can someone explain the execution differences between the two?
thanks.
2 Answers 2
When you do:
var x = function add(a,b,c,d){
// code...
}
add should only exist inside the function (and refer to itself). Outside of the function you need to use x, add will be undefined.
7 Comments
var x = function(a,b,c,d){} is exactly the same as function x(a,b,c,d){}.I believe that named functions are hoisted whereas function literals are not.
No reason to use both types though. I generally use an anonymous function literal as in my mind it maps them as objects, which they are.
addshould not work here. Does it actually work?