Last active
January 30, 2017 23:42
-
-
Save SeijiEmery/c121194484df17af546d to your computer and use it in GitHub Desktop.
Hifi Script.include() "bug" (not a bug, ppl just don't understand how js + Script.include() works!)
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
var foo; // define local 'foo' with value undefined | |
var foo; // redeclaration (bug, but probably not illegal) | |
foo = 3; // assign to 3 in file1 scope | |
Script.include('file2.js') // gets run in its own scope | |
print(foo); // => 3 | |
showFoo(); // => 1 (local foo bound in file2 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
print(foo); // global / external foo => 3 | |
var foo = 1; // shadow local variable named foo | |
print(foo); // => 1 | |
showFoo = function () { // global var, so gets exported to file1 | |
print(foo); // => 1 | |
// (non-parameterized variables get bound at function definition, not when called. | |
// We're using the file2 scope, not file1 (though it's available in file1 b/c we're writing to a global)) | |
} |
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
// | |
// Equivalent: | |
// | |
// Script.include(<file>) seems to be more-or less equivalent to copy + pasting the contents of <file> | |
// at the callsite, _but_ in it's own block / scope (which := functions in javascript (js function blocks | |
// get their own scope; no other construct (for / if / while / etc) does). | |
// file1.js | |
var foo; | |
var foo = 3; | |
(function () { | |
// file2.js | |
print(foo); // => 3 | |
var foo = 1; | |
print(foo) // => 1 | |
showFoo = function () { | |
print(foo); // => 1 | |
} | |
})(); | |
// cont file1.js | |
print(foo); // => 3 | |
showFoo(); // => 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment