Sei sulla pagina 1di 1

How what we have been discussing is used inside popular frameworks

for our purposes, a framework and a library are the same thing

Default Values

index.html
<html>
<head>

</head>
<body>
<script src="lib1.js"></script>
<script src="lib2.js"></script>
<script src="app.js"></script>
</body>
</html>

lib1.js
var libraryName = "Lib 1";

lib2.js
var libraryName = "Lib 2";

app.js
console.log(libraryName);

output:
Lib 2

what happens:
a. the 3 script tags are not creating new execution contexts. they are not
separating the code in any way. They are simply stacking the code one on top of
another then run them all as if they were inside a single file.

they were all treated as global variables in the gloabal execution context,
attached to the window object.

in the exxample, we have seen that libraries can collide/overwrite.

edit: in lib2.js
window.libraryName = window.libraryName || "Lib 2";

output:
Lib 1

Potrebbero piacerti anche