-3

I am learning JavaScript and I don't understand style of writting JS code which I read. I know "old" way but I didn't found manual where is explained this style of create and calling JS function. "New way":

// object
var dog = {
    color: 'brown',
    size: 'big'
};

//function to write dog size
(function(dog) {
    return dog.size;
})(dog);

From JavaScript manual I know (old way):

function prinSize(dog) {
    return dog.size
}
console.log(prinSize(dog));

Please could send me a link to JS doc, where is explained why

  • in JS code are on start and end of function brackets

  • why is possible after declare of function write brackets with .(dog) and with this you actually call the function with argument dog.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Poul D.
  • 59
  • 7
  • It's a [iife](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression), nothing new here – baao Sep 12 '16 at 18:39
  • What you're doing isn't "old way" vs. "new way". You still write functions the same as you always have. That first thing is an [immediately invoked function expression (IIFE)](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) and is a function that runs immediately. – Mike Cluck Sep 12 '16 at 18:39
  • The first code doesn't do anything. – JJJ Sep 12 '16 at 18:40
  • I do not understand the English "in JS code are on start and end of function brackets". Anyway, where did you see this "new style", which would be better called "meaningless, do-nothing style"? –  Sep 12 '16 at 18:40
  • @Juhana Not exactly -- it returns `dog.size` but then throws it away. –  Sep 12 '16 at 18:41
  • 1
    Yes, a.k.a. doesn't do anything. – JJJ Sep 12 '16 at 18:42
  • I don't know why someone marked this question with minus. I didn't understand style of writing the function and answer like "iife" is very helpful for me. – Poul D. Sep 12 '16 at 19:06

1 Answers1

2

To understand this better, consider an alternative way to write the number 1:

(function() { return 1; })()

What this does is define a function which returns 1, then immediately executes it with the () following it, so the whole thing evaluates to the number 1.

Of course, there is no reason to do this, I might just as well write 1.

So to do 1+2, I could write

(function() { return 1; })() + function() { return 1; }()

(I don't need the parentheses around the second function, since it is in a position syntactically where it could only be an expression.)

In the same sense, your function

(function(dog) { return dog.size; })(dog)

is exactly equivalent to just writing dog.size to start with. I don't know where you saw this, but standing on its own, it is ridiculous in two ways: first of all, as I said it is the same as dog.size, and second, it does not DO anything with the resulting size--to actually DO anything with the result, you'd need to write

var size = function(dog) { return dog.size; }();

but again, you might as well just write

var size = dog.size;

There are plenty of good reasons to use these so-called "IIFEs", or immediately-invoked function expressions, including hiding information inside them, but this is not one of them.