-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlesson5.js
43 lines (37 loc) · 1.13 KB
/
lesson5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Custom Pokemons Implementation
let Pokemons = {
// Method we want to replace
amount() {
// Call to database
},
// Method we want to stay intact
say(str) {
console.log('Your Pokedex says: ', str)
}
}
// Function using Pokemons class
function howsMyCollection() {
const size = Pokemons.amount()
if (size === undefined)
return Pokemons.say('Ooops, not sure how many you have')
if (size < 10)
return Pokemons.say('You only have a few, you need more')
if (size < 50)
return Pokemons.say('You have quite some favorites. Keep going')
return Pokemons.say('You are quite a collector')
}
// Keep an implementation of original implementation for potential use later on
const originalAmount = Pokemons.amount
// Change the output of the amounts function
function stubAmount(amount) {
Pokemons.amount = () => amount
}
// Testing function
function havePokemons(amount) {
stubAmount(amount)
howsMyCollection()
}
// A few tests to show outputs
havePokemons(5) // -- You only have a few, you need more
havePokemons(17) // -- You have quite some favorites. Keep going
havePokemons(100) // -- You are quite a collector