Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SEE THE CONSOLE LOGS TO UNDERSTAND
- class Car{
- /* constructor(){
- super() // If class is not extending anything dont call super it will give an error saying super not expected
- }*/
- setSound(sound){
- this.sound = sound;
- }
- getSound(){
- console.log(this.sound);
- return this.sound
- }
- }
- const newCar = new Car();
- newCar.setSound("yoo")
- newCar.getSound();
- const truck = {
- sound:"nooo00000000000",
- truckGetSound:newCar.getSound
- }
- truck.truckGetSound();
- //below code shows if the dot operator is not present
- const driveFunc = newCar.getSound; // did not invoked just assigned the function
- driveFunc();// error undefined
- // to solve this issue inside car class create a constructor and bind the function
- /* constructor(){
- this.getSound = this.getSound.bind(this);
- }*/
- // another option to solve this issue is to user arrow funtions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement