1

Nested self executing function java script throwing error for console.log() https://jsfiddle.net/vivek7189/co5oeqcg/

(function(a,b){
  console.log("value a:"+a +" value b:"+b)
   (function (a,b){
    console.log("value a:"+a +" value b:"+b)
   })(20,10)
})(30,40)
user3852580
  • 51
  • 1
  • 4
  • 2
    sometimes, javascript can't guess where to put a semicolon for you, and gets it wrong ... use semicolons correctly, and this sort of pain will go away ... basically js interprets this as `console.log('...')( ... )` ... and since console.log returns "undefined" and "undefined" is not a function, it fails ... there should be 4 `;` in your code - yo have none – Jaromanda X Feb 18 '16 at 14:11
  • @JaromandaX no need for 4 `;`, only for `console.log()` is enough.. – Anoop LL Feb 18 '16 at 14:15
  • I never said the code "needs" 4, I said there **should be 4** - even if you add the two you say are needed, javascript "pretends" another two are there as well ... oh, you can make the code run by adding a single `;` - or, by **shortening* the code by 1 character – Jaromanda X Feb 18 '16 at 14:21

3 Answers3

2

JavaScript engine treats

(function (a,b){
  console.log("value a:"+a +" value b:"+b);
})

as an arguments for function call, because engine thinks that first console.log("value a:"+a +" value b:"+b) returns a function once you put something into parenthesis after that, but its not... You have to make engine understand that you don't trying to call results of first console.log("value a:"+a +" value b:"+b) as a function, for example by adding ; or even + (whatever) after first console.log("value a:"+a +" value b:"+b) so that engine understand that its two statements rather then one

Vlad Ankudinov
  • 1,936
  • 1
  • 14
  • 22
0

Consider this piece of code

function foo(a) {
    return function(b) {
        return a+b;
    }
}

You can run this like

var x = foo(2);
var y = x(3); // y == 5

but this can be done "shorthand" like

var x = foo(2)(3); // x == 5

in your code, you have

console.log("value a:"+a +" value b:"+b);
  (function (a,b){
    console.log("value a:"+a +" value b:"+b);
  })(20,10)

Without semicolons, javscript guesses wrong and thinks you are trying to do

console.log('')( ... )

you can make the code run in a few ways

minimal - add a single character to your code

(function(a,b){
    console.log("value a:"+a +" value b:"+b); // add this colon
    (function (a,b){
        console.log("value a:"+a +" value b:"+b)
    }(20,10))
})(30,40)

make your code shorter

(function(a,b){
    console.log("value a:"+a +" value b:"+b)
    // use different syntax for the inner IIFE 
    // add a leading !, or +, or various other characters
    !function (a,b){
        console.log("value a:"+a +" value b:"+b)
    }(20,10)
})(30,40)

however, to avoid any pitfalls, put colons where they should be

v----- this one is optional, but may be required depending on preceding code
;(function(a,b){
    console.log("value a:"+a +" value b:"+b); // here
    (function (a,b){
        console.log("value a:"+a +" value b:"+b) // here
    }(20,10)); // here
})(30,40); // and here
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

put ; for console.log()

(function(a,b){
  console.log("value a:"+a +" value b:"+b);
  (function (a,b){
    console.log("value a:"+a +" value b:"+b);
  })(20,10)
})(30,40)
Anoop LL
  • 1,548
  • 2
  • 21
  • 32