function FizzBuzz() {
}
- First we create a function
FizzBuzz
that takes no arguments.
var fizzBuzz = new FizzBuzz();
var fizzBuzzLong = new FizzBuzz();
- Then we create two new variables
fizzBuzz
andfizzBuzzLong
. - Even though they look like instances of class
FizzBuzz
, they're not.FizzBuzz
is not a class, it's a function (constructor). - However, we can use the
FizzBuzz
function to instantiate objects (fizzBuzz
,fizzBuzzLong
) and tell them to share the same behaviour (eg. both can use the methods prototyped forFizzBuzz
- see below). fizzBuzz
andfizzBuzzLong
can have thir ownplay
methods (that will overwrite the FizzBuzz's prototypedplay
method.)- Let's see the first prototyped method for
FizzBuzz
.
FizzBuzz.prototype.play = function(number) {
if (this._isDivisibleBy(15, number)) {
return 'FizzBuzz';
} else if (this._isDivisibleBy(5, number)) {
return 'Buzz';
} else if (this._isDivisibleBy(3, number)) {
return 'Fizz';
} else {
return number;
}
}
- The first line -
FizzBuzz.prototype.play = function(number)
- creates a functionplay
which accepts one argument(number
) and can be called on objects which are instances ofFizzBuzz
- like our variablesfizzBuzz
andfizzBuzzLong
. - Now let's take a look at the
if
statement and explain it on the example of the first two lines:
if (this._isDivisibleBy(15, number)) {
return 'FizzBuzz';
}
this
in JS works likeself
in Ruby. As we will (later) call thisplay
method on instances ofFizzBuzz
(fizzBuzz
,fizzBuzzLong
), thisthis
keyword will refer to a particular instance upon which the method will be called.- Generally we need
this
keyword to call a function (_isDivisibleBy
) inside other function (play
). We cannot call a method on no object - it needs to refer to something - and that's why we need thisthis
:) _isDivisibleBy
is another function, called inside ofplay
function. It uses two arguments -15
(a divisor) and anumber
that was already passed in theplay
method.- In order to explain the
return
, we need to take a look at the_isDivisibleBy
method.
FizzBuzz.prototype._isDivisibleBy = function(divisor, number) {
return number % divisor === 0;
}
_isDivisibleBy
is also a function which can be used by instances ofFizzBuzz
.- The
_
before its name suggests that it shouldn't be called by the instances, but only used internally - in other methods. - But we can try to call it anyway (for the sole purpose of studying :D )
function FizzBuzz() {
}
var fizzBuzz = new FizzBuzz();
FizzBuzz.prototype._isDivisibleBy = function(divisor, number) {
return number % divisor === 0;
}
fizzBuzz._isDivisibleBy(2, 10) //true
-
_isDivisibleBy
function accepts two arguments that are passed into it inside theplay
method. -
If the
number % divisor === 0
, then the function returnstrue
, otherwise it returnsfalse
. -
Coming back to the
play
method - if_isDivisibleBy
returnstrue
,play
returnsFizzBuzz
. -
Now let's call the
play
method onfizzBuzz
andfizzBuzzLong
and see it in practice. -
First we call it on
fizzBuzz
, specifying that we're interested in the numbers from 1 to 20.
for (var i = 1; i <= 20; i++) {
console.log(fizzBuzz.play(i));
}
- As a result we get this:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
- Now let's call it on fizzBuzzLong and see numbers from 1 to 40.
for (var i = 1; i <= 40; i++) {
console.log(fizzBuzzLong.play(i));
}
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
- Now we can clearly see that the
this
keyword mentioned above referes to the particular instances of theFizzBuzz
and the predefined scope ofplay
function.