- Create a file in
src/lessons/globalcallednumberInventory.js. - In
numberInventory.jscreate a variable calledinventoryand assign it a value of[]. - In
numberInventory.jscreate a function calledaddNumberToInventorythat takes a number as an argument and pushes it to theinventoryarray. - In
src/lessons/global/index.htmladd a script tag before</body>with src of./numberInventory.js. - Open
src/lessons/global/index.htmlin your browser and interact with your inventory. - Create a file in
src/lessons/globalcalledstringInventory.js. - In
stringInventory.jscreate a variable calledinventoryand assign it a value of[]. - In
stringInventory.jscreate a function calledaddStringToInventorythat takes a string as an argument and pushes it to theinventoryarray. - In
src/lessons/global/index.htmladd a script tag before</body>with src of./stringInventory.js. - Open
src/lessons/global/index.htmlin 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/iifecallednumberInventory.js - In
numberInventory.jscreate an IIFE that defines a variable calledinventoryand assign it a value of[]. - In the IIFE in
numberInventory.jsdefine a function calledaddNumberToInventorythat takes a number as an argument and pushes it to theinventoryarray. - In
src/lessons/iife/index.htmladd a script tag before</body>to loadnumberInventory.js - Open
src/lessons/iife/index.htmlin your browser and interact with your inventory. - What is the value of
inventoryin the console? - In
src/lessons/iife/numberInventory.jscreate a binding on the window objectwindow.addNumberToInventoryand assign it the value of theaddNumberToInventoryfunction. - Open
src/lessons/iife/index.htmlin your browser and call your function from the console. - Create a file in
src/lessons/iifecalledstringInventory.js - In
stringInventory.jscreate an IIFE that defines a variable calledinventoryand assign it a value of[] - In the IIFE in
stringInventory.jsdefine a function calledaddStringToInventorythat takes a string as an argument and pushes it to theinventoryarray. - In
src/lessons/iife/stringInventory.jscreate a binding on the window objectwindow.addStringToInventoryand assign it the value of theaddStringToInventoryfunction. - Open
src/lessons/iife/index.htmlin your browser and call your function from the console. - What is the value of
inventoryin the console? - In
numberInventory.jscreate a binding on the window objectwindow.inventoryand assign it the value of theinventorybinding. - In
stringInventory.jscreate a binding on the window objectwindow.inventoryand assign it the value of theinventorybinding. - Open
src/lessons/iife/index.htmlin your browser and inspect the inventory
- Duplicate
numberInventory.jsandstringInventory.jsinsrc/lessons/iifeto thecommonjsdirectory. - Open the file in
src/lessons/commonjscalledrequire.js. - Follow the instructions in
require.jsto create arequirefunction, and acacheobject 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.htmladd a script tag before</body>to load all three scripts. - Call
requirewith the path to our scripts. - Open
src/lessons/commonjs/index.htmlin your browser and interact with your inventory.
- Create a file in
src/lessons/modulecallednumberInventory.js - In
numberInventory.jsdefine a variable calledinventoryand assign it a value of[]. - Use the
exportkeyword to export theinventoryvariable. - In
src/lessons/module/index.htmladd a script tag before</body>to loadnumberInventory.js - Open
src/lessons/module/index.htmlin 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.htmlin your browser and try to interact with your inventory. - What do you see in the console?
- In
src/lessons/module/index.htmladd a script tag before</body>and use theimportkeyword to import theinventoryvariable fromnumberInventory.js, then log the value ofinventoryto the console. - Open
src/lessons/module/index.htmlin your browser and try to interact with your inventory. - What do you see in the console?
- Create a file in
src/lessons/modulecalledstringInventory.js - In
stringInventory.jsdefine a variable calledinventoryand assign it a value of[]. - Use the
exportkeyword to export theinventoryvariable. - In
src/lessons/module/index.htmladd a script tag afternumberInventory.jsbut before our import script to loadstringInventory.js. - Import the
inventoryvariable fromstringInventory.jsand log the value ofinventoryto the console. - Open
src/lessons/module/index.htmlin your browser and try to interact with your inventory. - What do you see in the console?
- How can we fix this?