"name" variable in the code below outputs "undefined".
var name = "Baggins";
(function () {
// Outputs: "Original name was undefined"
console.log("Original name was " + name);
var name = "Underhill";
// Outputs: "New name is Underhill"
console.log("New name is " + name);
})();
The (function(){ })(); creates another scope, and variable defined outside of the scope is not accessible.
The example below is in a different situation.
var name = "Here";
function(){
console.log(name);
}
This is accessible. I need to dig into how they differently work.
コメント