Skip to content

Instantly share code, notes, and snippets.

@jaylandro
Last active January 29, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaylandro/40ee016463e627406a513d690b78ef88 to your computer and use it in GitHub Desktop.
Save jaylandro/40ee016463e627406a513d690b78ef88 to your computer and use it in GitHub Desktop.

JavaScript Fundamentals - Part 1

Scope/Hoisting

Var & Scope

var greeting = 'Hello'; 
console.log(greeting); //=> Hello

if (true) { 
  var greeting = 'Hi'; 
  console.log(greeting); //=> Hi
} 

console.log(greeting);   //=> Hi

Let & Scope

let greet = 'Hello'; 
console.log(greet); //=> Hello

if (true) { 
    let greet = 'Hi'; 
    console.log(greet); //=> Hi
} 

console.log(greet); //=> Hello 

   

   

   

   

   

   

   

   

   

   

   

   

   

   

Context/Binding/this

let modeObject = {
  show: function() {
    return this
  },
  showFatArrow: () => this
}

modeObject.show(); //=> { show: ƒ, showFatArrow: ƒ }

modeObject.showFatArrow(); //=> Window

   

   

   

   

   

   

   

   

   

   

   

   

   

   

Prototype/Constructors/Classes

Class inheritance

class Shopkeeper {
    talk() {
        console.log(this.lines[Math.floor(Math.random() * this.lines.length)]);
    }
}
Shopkeeper.prototype.lines = ["Can I interest you in some wares?", "Welcome, weary traveler"];
Problem
const artifactDealer = new Shopkeeper();
const clothingSeller = new Shopkeeper();


artifactDealer.lines = ["Grumble, grumble."];
clothingSeller.talk() => ?

   

   

   

   

   

Problem
const artifactDealer = new Shopkeeper();
const clothingSeller = new Shopkeeper();


artifactDealer.lines.push("Grumble, grumble.");
clothingSeller.talk() => ?

   

   

   

   

   

Problem
const artifactDealer = new Shopkeeper();
const clothingSeller = new Shopkeeper();

artifactDealer.lines = ["Grumble, grumble."];

delete artifactDealer.lines;
artifactDealer.lines => ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment