Giter Site home page Giter Site logo

objects-scope-hoisting's Introduction

Scope, hoisting and compartmentalization

Answer the following:

In you own words, explain the concepts of scope, hoisting, compartmentalization. Scope is global vs local variables. Hoisting is top to bottom nature of the Javascript document. It is the moving of objects to where they need to be. Strict makes it so it will not do it. Compartmentalization is where each object is placed to make it run the right way.

Provide examples for all three, below:

Scope:

(function(){ "use strict";

var x = "I'm a local variable"; //console.log(y)

function scopeThis(){ var y = "I'm a global variable"; console.log(x); } scopeThis(); })();

Hoisting:

(function() { "use strict";

var goodStanding = true; var monthsActive = 18;

//Do not modify 'name' globaly. var name = null;

var benefit = {} benefit["credit"] = 50; benefit["discount"] = 5;

var accountCheck = function() {

name = "James";

var greeting = function() {

  return "Hello " + name + ". Here is the status of your account."
}

function accountStat() {

  if (goodStanding == true && monthsActive >= 12) {

    return offerDiscount(name);

  } else if (goodStanding == false) {

    return "Please make a payment within 7 days or your service will be terminated, forever."

  } else if (monthsActive <= 12) {

    var timeFrame = 12 - monthsActive;
    var months;

    if (timeFrame == 1) {

      months = "month";
    } else {

      months = "months"
    }

    return "You are " + timeFrame + " " + months + " from getting a special discount!"
  }

  function offerDiscount() {

    return "Thank you for your loyalty. You've been a member for " + monthsActive + " " + "months . You next bill will reflect a $" + benefit.credit + " credit and a " + benefit.discount + "% discount going forward.";
  }
}
return greeting() + " " + accountStat();

} accountCheck(); console.log("#8 accountCheck():", accountCheck()); console.assert(name == "James", "Test failed. You should set 'name' to 'james' from within accountCheck()"); console.assert(accountCheck() == "Hello James. Here is the status of your account. Thank you for your loyalty. You've been a member for 18 months . You next bill will reflect a $50 credit and a 5% discount going forward.", "Test failed. It returned: " + accountCheck());

})();

Compartmentalization:

(function() { "use strict"; var multiply = 2 * 8;

function duplicate() { var multiply = 2 * 10; };

duplicate();

console.log( "multiply", multiply ); console.assert( multiply == 16, "Test failed. How can we isolate duplication()" ); })();

objects-scope-hoisting's People

Contributors

stcaldwell avatar

Watchers

James Cloos avatar

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.