-9

The function is:

(function (x,y) {return x+y})('foo')

I am learning javascript.

I do not know what this sentence mean. It seems to be a function. However, when I use it as

foo(1,2)

It gives errors. could anyone tell me what that sentence means? is it a function or just a wrong statement? If it is a function, how can I use it? Or it is

There is a similar function from a demo of facebook API, which is

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

I don't understand this code. Is it a function which takes document, 'script', 'facebook-jssdk' as input? if it is, what is the function name? Thanks!

tqjustc
  • 3,624
  • 6
  • 27
  • 42

1 Answers1

3

You have an anonymous function there which is accepting two arguments, x and y. It is returning the combination of those two. The function is being called with 'foo' as a string, and as a result the return is 'fooundefined'.

As there was nothing to store this assignment, and y was unused, it seems slightly pointless to take this approach. Normally an immediately invoked function expression will have a side affect or return a constructed object to a variable.

(function(x,y){
  document.getElementById(x).innerHTML = y;
})('foo',5)

or

var add = (function(x,y){return x+y})(1,2);

edit

In response to your edit, if you are going to name a function it needs to come before the parameters, and it needs to be available outside of the expression scope. So to define foo you would use

function foo(x,y){
 return x+y;
}

and then you could use it

var add = foo(1,2);
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • In this: `(function (x,y) {return x+y})('foo')` , could I say `(function (x,y) {return x+y})` is the function body and `foo` is the name of function ? – tqjustc May 14 '14 at 23:38
  • That would not be correct. `'foo'` is the argument being passed into an anonymous function with the parameters x and y. Since only foo is passed, x receives foo and y becomes undefined. The function has no name, and is anonymous. – Travis J May 15 '14 at 01:27
  • We can not re-use the anonymous function, right? then why was it created? why not use the statements directly instead of creating an anonymous function? Thanks. – tqjustc May 15 '14 at 04:11
  • 1
    If the anonymous function is not stored in a variable then it cannot be re-used (unless it is nested inside of another function in which case it will be executed when evaluated). The reason to use the anonymous function is to avoid the global namespace. Once your variables are inside a function they are scoped to that function. This means they will not interfere nor be interfered with by other variables on the page which may have shared the same scope and name. It is good to avoid polluting the global namespace. You can see more here: http://stackoverflow.com/a/13352212/1026459 – Travis J May 15 '14 at 16:36