- Create a file in
src/lessons/global
callednumberInventory.js
. - In
numberInventory.js
create a variable calledinventory
and assign it a value of[]
. - In
numberInventory.js
create a function calledaddNumberToInventory
that takes a number as an argument and pushes it to theinventory
array. - In
src/lessons/global/index.html
add a script tag before</body>
with src of./numberInventory.js
. - Open
src/lessons/global/index.html
in your browser and interact with your inventory. - Create a file in
src/lessons/global
calledstringInventory.js
. - In
stringInventory.js
create a variable calledinventory
and assign it a value of[]
. - In
stringInventory.js
create a function calledaddStringToInventory
that takes a string as an argument and pushes it to theinventory
array. - In
src/lessons/global/index.html
add a script tag before</body>
with src of./stringInventory.js
. - Open
src/lessons/global/index.html
in your browser and interact with your inventory. - What's going on here? Why can't we have two inventories?
// IIFE syntax
(function() {
// code goes here
})();
- Create a file in
src/lessons/iife
callednumberInventory.js
- In
numberInventory.js
create an IIFE that defines a variable calledinventory
and assign it a value of[]
. - In the IIFE in
numberInventory.js
define a function calledaddNumberToInventory
that takes a number as an argument and pushes it to theinventory
array. - In
src/lessons/iife/index.html
add a script tag before</body>
to loadnumberInventory.js
- Open
src/lessons/iife/index.html
in your browser and interact with your inventory. - What is the value of
inventory
in the console? - In
src/lessons/iife/numberInventory.js
create a binding on the window objectwindow.addNumberToInventory
and assign it the value of theaddNumberToInventory
function. - Open
src/lessons/iife/index.html
in your browser and call your function from the console. - Create a file in
src/lessons/iife
calledstringInventory.js
- In
stringInventory.js
create an IIFE that defines a variable calledinventory
and assign it a value of[]
- In the IIFE in
stringInventory.js
define a function calledaddStringToInventory
that takes a string as an argument and pushes it to theinventory
array. - In
src/lessons/iife/stringInventory.js
create a binding on the window objectwindow.addStringToInventory
and assign it the value of theaddStringToInventory
function. - Open
src/lessons/iife/index.html
in your browser and call your function from the console. - What is the value of
inventory
in the console? - In
numberInventory.js
create a binding on the window objectwindow.inventory
and assign it the value of theinventory
binding. - In
stringInventory.js
create a binding on the window objectwindow.inventory
and assign it the value of theinventory
binding. - Open
src/lessons/iife/index.html
in your browser and inspect the inventory
- Duplicate
numberInventory.js
andstringInventory.js
insrc/lessons/iife
to thecommonjs
directory. - Open the file in
src/lessons/commonjs
calledrequire.js
. - Follow the instructions in
require.js
to create arequire
function, and acache
object that are available in the global scope. - In our inventory scripts, let's set the exports porperty of the cache['pathname'] to an object with properties equal to our interface.
- In
src/lessons/commonjs/index.html
add a script tag before</body>
to load all three scripts. - Call
require
with the path to our scripts. - Open
src/lessons/commonjs/index.html
in your browser and interact with your inventory.
- Create a file in
src/lessons/module
callednumberInventory.js
- In
numberInventory.js
define a variable calledinventory
and assign it a value of[]
. - Use the
export
keyword to export theinventory
variable. - In
src/lessons/module/index.html
add a script tag before</body>
to loadnumberInventory.js
- Open
src/lessons/module/index.html
in your browser and try to interact with your inventory. - What do you see in the console?
- add
type="module"
to the script tag insrc/lessons/module/index.html
- Open
src/lessons/module/index.html
in your browser and try to interact with your inventory. - What do you see in the console?
- In
src/lessons/module/index.html
add a script tag before</body>
and use theimport
keyword to import theinventory
variable fromnumberInventory.js
, then log the value ofinventory
to the console. - Open
src/lessons/module/index.html
in your browser and try to interact with your inventory. - What do you see in the console?
- Create a file in
src/lessons/module
calledstringInventory.js
- In
stringInventory.js
define a variable calledinventory
and assign it a value of[]
. - Use the
export
keyword to export theinventory
variable. - In
src/lessons/module/index.html
add a script tag afternumberInventory.js
but before our import script to loadstringInventory.js
. - Import the
inventory
variable fromstringInventory.js
and log the value ofinventory
to the console. - Open
src/lessons/module/index.html
in your browser and try to interact with your inventory. - What do you see in the console?
- How can we fix this?