Last active
December 15, 2023 14:48
-
-
Save mariusschulz/e68330ca340d14341a8fff9a014a113c to your computer and use it in GitHub Desktop.
Code for my egghead.io course "Understand JavaScript's this Keyword in Depth"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "use strict"; | |
console.log(this === window); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function func() { | |
"use strict"; | |
console.log(this === undefined); | |
} | |
func(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "use strict"; | |
function Person(firstName, lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
const person = Person("Jane", "Doe"); | |
console.log(person); | |
console.log(window.firstName); | |
console.log(window.lastName); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(firstName, lastName) { | |
console.log(this); | |
this.firstName = firstName; | |
console.log(this); | |
this.lastName = lastName; | |
console.log(this); | |
// return this; | |
} | |
const person = new Person("Jane", "Doe"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const person = { | |
firstName: "John", | |
sayHi() { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
} | |
}; | |
person.sayHi(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const person = { | |
firstName: "John", | |
sayHi() { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
} | |
}; | |
const greet = person.sayHi; | |
greet(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sayHi() { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
} | |
const person = { | |
firstName: "Jane", | |
lastName: "Doe" | |
}; | |
sayHi.apply(person); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const person = { | |
firstName: "John", | |
sayHi() { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
} | |
}; | |
setTimeout(person.sayHi /* .bind(person) */, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const outerThis = this; | |
const func = () => { | |
console.log(this === outerThis); | |
}; | |
func(); | |
func.call(null); | |
func.apply(undefined); | |
func.bind({})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const counter = { | |
count: 0, | |
incrementPeriodically() { | |
setInterval(() => { | |
console.log(++this.count); | |
}, 1000); | |
} | |
}; | |
counter.incrementPeriodically(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
constructor(firstName, lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
sayHi() { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
} | |
} | |
const person = new Person("John", "Doe"); | |
person.sayHi(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
sayHi = () => { | |
console.log(`Hi, my name is ${this.firstName}!`); | |
}; | |
constructor(firstName, lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
} | |
const person = new Person("John", "Doe"); | |
const greet = person.sayHi; | |
greet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment