Skip to content Skip to sidebar Skip to footer

Javascript OOP - Inheritance, Prototyping, Callback Function

I'm trying to use OOP in Javascript with inheritance, prototyping and callback functions. Would you please have a look at my JSfiddel http://jsfiddle.net/Charissima/5g6GV/. The fir

Solution 1:

You've put the two functions in an array, so when called, this get changed. You could use function bind :

   var drivingFunctions = [car.drive.bind(car), raceCar.drive.bind(raceCar)];

Here is an example to help you understand:

function Man(name){
    this.name = name;
    this.getName = function(){
      return this.name;  
    };
}
var man = new Man('toto');
var a = [man.getName];
console.log(a[0]());//undefined
a.name = 'titi';
console.log(a[0]());//titi, because this refers to the array.

Post a Comment for "Javascript OOP - Inheritance, Prototyping, Callback Function"