Skip to content

Instantly share code, notes, and snippets.

@polotek
Forked from jaylandro/JavaScript-Fundamentals-Part-1.md
Last active March 10, 2024 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polotek/727cb0a6492a188d29ffc0b4544b106d to your computer and use it in GitHub Desktop.
Save polotek/727cb0a6492a188d29ffc0b4544b106d to your computer and use it in GitHub Desktop.

JavaScript Fundamentals

Part 1: Variables

Var & Scope

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

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

console.log(greeting);   // prints: Hi

   

   

   

   

   

   

Let & Scope

let greeting = 'Hello';
console.log(greeting); // prints: Hello

if (true) {
    let greeting = 'Hi';
    console.log(greeting); // prints: Hi
}

console.log(greeting); // prints: Hello

   

   

   

   

   

   

   

   

   

   

   

   

   

   

Part 2: Function Binding & Arrow Functions

const modeObject = {
  show: function() {
    return this
  },
  showArrow: () => this
}

modeObject.show(); // prints: { show: ƒ, showArrow: ƒ }

modeObject.showArrow(); // prints: Window

   

   

   

   

   

   

   

   

   

   

   

   

   

   

Classes & Prototypes

class Shopkeeper {
    talk() {
        // Return a randon item from the array
        const words = this.lines[Math.floor(Math.random() * this.lines.length)];
        console.log(words);
    }
}
Shopkeeper.prototype.lines = ["Can I interest you in some wares?", "Welcome, weary traveler"];

   

   

   

   

   

   

Problem
class Shopkeeper {
    talk() {
        // Return a randon item from the array
        const words = this.lines[Math.floor(Math.random() * this.lines.length)];
        console.log(words);
    }
}
Shopkeeper.prototype.lines = ["Can I interest you in some wares?", "Welcome, weary traveler"];

var artifactDealer = new Shopkeeper();
var clothingSeller = new Shopkeeper();

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

   

   

   

   

   

   

Problem
class Shopkeeper {
    talk() {
        // Return a randon item from the array
        const words = this.lines[Math.floor(Math.random() * this.lines.length)];
        console.log(words);
    }
}
Shopkeeper.prototype.lines = ["Can I interest you in some wares?", "Welcome, weary traveler"];

var artifactDealer = new Shopkeeper();
var clothingSeller = new Shopkeeper();

artifactDealer.lines.push("Grumble, grumble.");
clothingSeller.talk() // prints: ?

   

   

   

   

   

   

Problem
class Shopkeeper {
    talk() {
        // Return a randon item from the array
        const words = this.lines[Math.floor(Math.random() * this.lines.length)];
        console.log(words);
    }
}
Shopkeeper.prototype.lines = ["Can I interest you in some wares?", "Welcome, weary traveler"];

var artifactDealer = new Shopkeeper();
var clothingSeller = new Shopkeeper();

artifactDealer.lines = ["Grumble, grumble."];
artifactDealer.talk() // prints: ?

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