0

Why does

var spo = function(){
   var qq = function(){};
}

throw the error undefined is not a function when

var spo = function(){
   function qq(){};
}

does not?


More elaborate example throws the exact error: TypeError: undefined is not a function

node version v0.6.10 compiled on Ubuntu

spo = function(car){

        var q = 10;
        var s = 'fraggle';

        var qq = function(){
                console.log(s);
        }

        (function(){

                while(q){
                        console.log(q);
                        q--;
                }
                        qq.call();
                        car.call();
        })();


};


spo(function(){console.log('as intended');});
HellaMad
  • 5,294
  • 6
  • 31
  • 53
Tegra Detra
  • 24,551
  • 17
  • 53
  • 78
  • In which browser? How are you calling `qq`? – RobG Feb 14 '12 at 06:11
  • Not sure what you mean. Could you provide so code to show when the undefined error occurs and when it does not? – LandonSchropp Feb 14 '12 at 06:11
  • Not knowing what your functions are actually doing... In the first case, the function is evaluated and assigned to qq. In the second case, qq is never actually called - it's only being defined. – Bill Feb 14 '12 at 06:12
  • Also works for me. Please elaborate – Ed Heal Feb 14 '12 at 06:14
  • I am using NODEJS some version of v8 let me make sure I am not doing something else wrong. I just put a super generic version of my problem. Let me get more information. I may have even found a real bug! – Tegra Detra Feb 14 '12 at 06:45
  • Somebody please correct me if I'm wrong, but does this have to do with qq being declared locally in the first answer and globally in the second answer? If so then is qq undefined in the second answer because it's not declared locally in the closure scope? Just a guess. – LandonSchropp Feb 14 '12 at 07:01
  • No it carries down the scope chain. – Tegra Detra Feb 14 '12 at 07:04
  • OK Ok after causing all this trouble I figured out that runjs is mucking up for some reason it does work sorry. – Tegra Detra Feb 14 '12 at 07:06

3 Answers3

2

In this qq is assigned the function dynamically.

var spo = function(){
   // qq is undefined here
   var qq = function(){};
   // qq is defined here
}

On the other hand, in this case, qq is defined as a function which is visible everywhere in spo

var spo = function(){
   // qq is defined here
   function qq(){};
   // qq is defined here also
}

EDIT: In your updated code the actual problem is visible, no semi colon after function which results in a wrong statement like given below

var qq = function(){}()(); // This is causing TypeError

Put a semi colon after function.

var qq = function(){
    console.log(s);
};  // you missed this semi colon

(function(){

     while(q){
         console.log(q);
         q--;
     }
     qq.call();
     car.call();
})();
Diode
  • 24,570
  • 8
  • 40
  • 51
0

You need a semicolon after your definition of qq. The engine is attempting to execute the result of calling your qq function with the argument of your anonymous function in the parentheses - if that's difficult to see, imagine removing the newlines and spaces separating the qq definition and the self-executing function.

var qq = function() {
}(function() {})();
Twisol
  • 2,762
  • 1
  • 17
  • 17
-3

Try changing

qq.call(); 

to

qq();

As qq is a just a function and therefore not an "object" with the method call.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Actually, in Javascript functions *are* objects with methods. You are correct that `.call()` isn't necessary here, though. – Twisol Feb 14 '12 at 07:03
  • It can be argued that Javascript has no concept of objects - see http://stackoverflow.com/questions/107464/is-javascript-object-oriented – Ed Heal Feb 14 '12 at 07:13