Coder Social home page Coder Social logo

node-global-lab's Introduction

Use REPL and Global

Objectives

  1. Use REPL
  2. Access global
  3. Use explicit vs. implicit declarations

Introduction

Scoping is about defining and accessing variables/objects/functions. Here's a small example which can illustrate how dangerous it can be to not understand scope: our program uses a variable named list and module A uses a variable named list. When we import module A, the original list of the program is lost!

Understanding global and scoping will give you a good foundation for future development.

In this lab, we're going to be using the Node REPL to create several bank accounts and users in different scopes.

Instructions

  1. Enter Node REPL
  2. Print the global object
  3. Print the process object (global.process)
  4. Create a global variable user explicitly with global.user = {admin: false}
  5. Create a global variable implicitly with var account = {balance: 1000}
  6. Open a new REPL and check for values of user and account (ReferenceError means the variable is undefined)
  7. Go back to the first REPL and print user and account with global.user and global.account
  8. Delete user with delete user (true) and print it again. You should get ReferenceError because you deleted user.
  9. Delete account with delete account (false) and print it again ({balance: 1000})
  10. Exit REPL
  11. Contemplate on the fact that user and account reacted differently to the delete operator. Read the extra info if you forgot how delete works (it's JavaScript and not exclusive to Node).

Extra Info

To understand the discrepancy, you need to know how delete works and what was the difference between user and account in the ways they were created (explicit with global vs. implicit with var ).

View node-global-lab on Learn.co and start learning to code for free.

View Node Global Lab on Learn.co and start learning to code for free.

node-global-lab's People

Contributors

annjohn avatar azat-co avatar franknowinski avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-global-lab's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.