1

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.

3
  • 1
    add should not work here. Does it actually work? Commented Aug 24, 2011 at 19:43
  • @primvdb: It might in IE. IE has bugs regarding named function expressions. Commented Aug 24, 2011 at 19:44
  • @primvdb: It works in firefox 3.6 and I am testing with firebug.. Commented Aug 24, 2011 at 20:43

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

add is not coming as undefined. Check in FF
@Anna: Seems browsers all handle this differently. I suggest not declaring functions like this, because the behavior as you can see is not the same.
Can one say that function literal definitions are almost equivalent to anonymous function then? Is there no difference in terms of usage?
@Anna: var x = function(a,b,c,d){} is exactly the same as function x(a,b,c,d){}.
@Anna: What do you mean by "function literal definitions are almost equivalent to anonymous function"?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.