Poul D. Poul D. - 1 year ago 95
Javascript Question

JavaScript advanced function call

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;

From JavaScript manual I know (old way):

function prinSize(dog) {
return dog.size

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.

Answer Source

This is known as self-invoked function, or as recently discussed, should actually be called immediately-invoked. It is nothing to be confused about. It is just like any other function, however because it gets called on declaration, it does not need a name to be referenced. However, you can still pass through arguments like any other function. It can be used for a number of reasons, some a little more complex then this in itself.

Some use it simply because it conforms to asynchronous methodologies ...which may be the pattern of choice in an app.

Some use it as they would prefer their one off function to be called once its declared, so instead of

function hello(name){


You can just do

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download