Skip to content

Instantly share code, notes, and snippets.

@kaumac
Created September 29, 2016 16:21
Show Gist options
  • Save kaumac/ace36258b37d34c2f25ae108290e550d to your computer and use it in GitHub Desktop.
Save kaumac/ace36258b37d34c2f25ae108290e550d to your computer and use it in GitHub Desktop.
Weird Mocha behavior regarding scope
/*
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