Skip to content

Instantly share code, notes, and snippets.

View sergiopichardo's full-sized avatar
📚
Building

Sergio Pichardo sergiopichardo

📚
Building
View GitHub Profile
@sergiopichardo
sergiopichardo / copiedFunctionLosesContext.js
Created July 30, 2021 01:23
A function can lose its context when a method is copied (extracted) and invoked as a function
let sergiosTasks = {
tasks: ['study oop javascript', 'prepare for sysops exam', 'read linux book', 'write article'],
getTasks() {
console.log('Today I have to:')
this.tasks.forEach((task, index) => {
console.log(`(${index + 1}) ${task}`);
})
}
}
@sergiopichardo
sergiopichardo / onceBoundAContextCannotBeChanged.js
Created July 29, 2021 14:18
Once a function/method has been permanently bound, its context cannot be changed
let schittsCreekData = {
data: {
cast: ["Daniel Levy", "Eugine Levy", "Annie Murphy", "Catherine O'Hara"],
category: 'TV Series',
episodes: 80,
releaseDate: '2015',
genre: 'Comedy',
keywords: ['motel', 'small', 'family', 'riches to rages']
}
}
@sergiopichardo
sergiopichardo / usingBindMethod.js
Last active July 30, 2021 15:42
Using the bind method to permanently bind the execution context of a method
const user = {
firstName: 'Roland',
lastName: 'Schitt',
username: function() {
return this.firstName + this.lastName;
}
}
// const getUsername = user.username; // NaN
const getUsername = user.username.bind(user);
const roland = {
fullName: 'Roland Schitt',
job: "Schitt's Creek Mayor"
}
function getInformation(occupation) {
return `${this.fullName}, ${occupation}.`;
}
console.log(getInformation.apply(roland, [roland.job]));
@sergiopichardo
sergiopichardo / applyMethodPattern.js
Created July 29, 2021 14:07
Apply Method Pattern
// pattern for a function using the `apply` method
aFunction.apply(context, [arg1, arg2, arg3])
// pattern for a method using the `apply` method
anObject.aMethod.apply(context, [arg1, arg2, arg3])
@sergiopichardo
sergiopichardo / usingCallWithoutContext.js
Created July 29, 2021 14:04
When `call` is used without an execution context, the context is the global object
global.roseFamily = ['David', 'Alexis', 'Moira', 'Johnny'];
function welcome() {
this.roseFamily.forEach(member => {
console.log(`Welcome to Schitt's creek ${member}!`);
});
}
welcome.call()
function User(name, email, course) {
this.name = name;
this.email = email;
this.course = course;
}
function Student(name, email, course, registered) {
User.call(this, name, email, course);
this.registered = registered;
}
@sergiopichardo
sergiopichardo / usingCallToExplicitlySetContext.js
Created July 29, 2021 13:58
Using the `call` method to explicitly set the execution context of a function
const data = {
firstName: 'Johnny',
lastName: 'Rose',
greeting: 'Good morning!'
}
function introduce() {
return `${this.greeting}! I'm ${this.firstName} ${this.lastName}!`;
}
@sergiopichardo
sergiopichardo / callMethodPattern.js
Created July 29, 2021 13:55
Function.prototype.call method pattern
// pattern for a function using the `call` method
aFunction.call(context, arg1, arg2, arg3)
// pattern for a method using the `call` method
anObject.aMethod.call(context, arg1, arg2, arg3)
@sergiopichardo
sergiopichardo / thisIsUndefinedWithStrictMode.js
Created July 29, 2021 13:54
In a function with strict mode, `this` is `undefined`
"use strict";
const user = {
firstName: 'Ronald',
lastName: 'Schitt',
username: function() {
return this.firstName + this.lastName;
}
}