Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // SEE THE CONSOLE LOGS TO UNDERSTAND
  2. class Car{
  3.  
  4. /* constructor(){
  5. super() // If class is not extending anything dont call super it will give an error saying super not expected
  6. }*/
  7.  
  8. setSound(sound){
  9. this.sound = sound;
  10. }
  11. getSound(){
  12. console.log(this.sound);
  13. return this.sound
  14. }
  15. }
  16.  
  17. const newCar = new Car();
  18. newCar.setSound("yoo")
  19. newCar.getSound();
  20.  
  21. const truck = {
  22. sound:"nooo00000000000",
  23. truckGetSound:newCar.getSound
  24. }
  25.  
  26. truck.truckGetSound();
  27.  
  28. //below code shows if the dot operator is not present
  29. const driveFunc = newCar.getSound; // did not invoked just assigned the function
  30. driveFunc();// error undefined
  31.  
  32. // to solve this issue inside car class create a constructor and bind the function
  33. /* constructor(){
  34. this.getSound = this.getSound.bind(this);
  35. }*/
  36. // another option to solve this issue is to user arrow funtions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement