Can someone explain to me why x is undefined? Shouldn't be 123?
doSomething = (function(x) {
alert(x)
})();
doSomething(123);
doSomething isn't a function. It's undefined.
doSomething = (function(x) {
alert(x)
})();
This declares an anonymous function, immediately executes it (that's what the () does), then sets doSomething to the return value - undefined. Your anonymous function takes one parameter (x), but nothing is passed to it, so x is undefined.
You probably want this:
doSomething = function(x) {
alert(x)
};
doSomething(123);
Wouldn't this be the better way? let it init, then call it passing an argument?
doSomething = (function(x) {
return(
init = function(x){
alert(x)
}
)
})();
doSomething("test")
init? Why does the main anonymous take a parameter x? If you really wanted to do it this way, do this: doSomething = (function(){ return function(x){ alert(x); }; })();doSomething = function(x){ alert(x); };?var before your local vars!