This question is more on javascript principle.
function done(){ console.log('done defined with `function done(){ ...`'); }
var done = function(){ console.log('done defined with `var done = ...`'); }
done = function(){ console.log('without `var`, just `done = ...`'); }
If defined right inside <script>
tags, will they all do the same thing, right?
But if I place them in a closure (function(){
function definintion goes here }())
will any of these three types override either the globally defined function done() or any other done() functions that are defined inside their respective closures?
If the question above doesn't make sense, here's to rephrasing;
- is the following code supposed to do the same thing in any JS runtime?
eval
-ing code anywhere executes that particular code within the context or the global scope?how can a
setTimeout
call be configured so that the code between its "quotes" executes to inside the scope where that particularsetTimeout
has been called (please see second timeout insidefor
below)? I mean is there any other way besides defining window.blabla functions and telling them to delete themselves after they run?function done(d){ console.log('cha cha cha: '+d); } setTimeout( function(){ done(2); }, 3500 ); for(i=0; i<10; i++){ (function(){ done = function(x){ console.log('done #'+i+' sais: '+x); } setTimeout(function(){ done(i*2); },2500); setTimeout(function(){ done(i*2); }.toString()+'(); ',2500); }()); }