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 Country(name, traveled) { | |
this.name = name ? name : 'United Kingdom'; | |
this.traveled = Boolean(traveled); // transform to a boolean | |
} | |
Country.prototype.travel = function() { | |
this.traveled = true; | |
}; | |
// Constructor invocation | |
var france = new Country('France', false); | |
// Constructor invocation |
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 City { | |
constructor(name, traveled) { | |
this.name = name; | |
this.traveled = false; | |
} | |
travel() { | |
this.traveled = true; | |
} | |
} | |
// Constructor invocation |
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
var rabbit = { name: 'White Rabbit' }; | |
function concatName(string) { | |
console.log(this === rabbit); // => true | |
return string + this.name; | |
} | |
// Indirect invocations | |
concatName.call(rabbit, 'Hello '); // => 'Hello White Rabbit' | |
concatName.apply(rabbit, ['Bye ']); // => 'Bye White Rabbit' |
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
var numbers = { | |
array: [3, 5, 10], | |
getNumbers: function() { | |
return this.array; | |
} | |
}; | |
// Create a bound function | |
var boundGetNumbers = numbers.getNumbers.bind(numbers); | |
boundGetNumbers(); // => [3, 5, 10] | |
// Extract method from object |
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 Point { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
log() { | |
console.log(this === myPoint); // => true | |
setTimeout(()=> { | |
console.log(this === myPoint); // => true | |
console.log(this.x + ':' + this.y); // => '95:165' |
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 Animal { | |
constructor(name) { | |
this.speed = 0; | |
this.name = name; | |
} | |
run(speed) { | |
this.speed += speed; | |
alert(`${this.name} runs with speed ${this.speed}.`); | |
} | |
stop() { |
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 Rabbit { | |
constructor(name) { | |
this.name = name; | |
} | |
hide() { | |
alert(`${this.name} hides!`); | |
} | |
} | |
let rabbit = new Rabbit("My rabbit"); |
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 Rabbit { | |
constructor(name) { | |
this.name = name; | |
} | |
hide() { | |
alert(`${this.name} hides!`); | |
} | |
} | |
let rabbit = new Rabbit("My rabbit"); |
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 Animal { | |
constructor(name) { | |
this.speed = 0; | |
this.name = name; | |
} | |
run(speed) { | |
this.speed += speed; | |
alert(`${this.name} runs with speed ${this.speed}.`); | |
} | |
stop() { |
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 Animal { | |
constructor(name) { | |
this.speed = 0; | |
this.name = name; | |
} | |
run(speed) { | |
this.speed += speed; | |
alert(`${this.name} runs with speed ${this.speed}.`); | |
} | |
stop() { |