I am trying to understand closures. In the code below, I create an instance of the constructor function Ninja
and I call the instance kawazaki
. I expected to be able to access the methods of Ninja
. However, I am getting a TypeError: Object #<Ninja> has no method 'feints'
instead.
The output I expected was 1
.
Here is my code:
function Ninja() {
var feints = 0;
function getFeints() {
return feints;
}
function feints() {
feints++;
}
}
var kawazaki = new Ninja();
kawazaki.feints();
console.log(kawazaki.getFeints());