Created
September 29, 2016 16:21
-
-
Save kaumac/ace36258b37d34c2f25ae108290e550d to your computer and use it in GitHub Desktop.
Weird Mocha behavior regarding scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Running code on Chrome's console, the output on line 10 is "undefined" as we're wrapping everything inside an IIFE, | |
which creates a new scope so nothing is exposed to the global scope unless its intentional like we did on line 9. | |
*/ | |
(function() { | |
let myObject = { | |
name : 'Jory' | |
}; | |
window.myWindowObject = { | |
name: 'Kaue' | |
} | |
return myObject; | |
}()); | |
console.log(window.myObject); // undefined | |
console.log(window.myWindowObject); // Object {} | |
/* | |
For some reason an IIFE does not create a new scope in Mocha. | |
So everything inside an IIFE is being exposed to the global scope | |
*/ | |
(function() { | |
let myObject = { | |
name : 'Jory' | |
}; | |
window.myWindowObject = { | |
name: 'Kaue' | |
} | |
return myObject; | |
}()); | |
console.log(window.myObject); // Object {} | |
console.log(window.myWindowObject); // Object {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment