I have defined the function:
var Test = function(){
};
Test.prototype={
getColor: function(){
return "red";
},
createCar: function(){
var color = this.getColor(); //ERROR: this.getColor is not a function
...
},
getCar: function(){
return new CarFactory(1, this.createCar);
}
}
As you saw, I have defined three prototype functions: getColor() , createCar() and getCar().
Inside createCar() function I called getColor(),
In function getCar(), I use this.createCar as a parameter of CarFactory constructor. I got the error:
"this.getColor is not a function"
in the above indicated place, why this error? How to get rid of this error?
I think you might not be making a Test
object and invoking it properly. I pasted your snippet into a test page, then added:
var obj = new Test();
console.log(obj.getColor());
// Outputs 'red'
obj.createCar();
// Does not throw an error.
Replacing your ...
with console.log(color);
revealed the correct result 'red' in my test.