Advertisement
makispaiktis

This keyword

Oct 21st, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let obj = {
  2.     name:"Chair",
  3.     age:5,
  4.     material:"wood",
  5.     stats(){
  6.         console.log(`Name = ${this.name}, age = ${this.age}, material = ${this.material}`);
  7.     },
  8.     value_of_this(){
  9.         console.log(`Value of this keyword = ${this}\nType of this keyword = ${typeof this}`);
  10.     },
  11.     _country:"Italy",
  12.     get country(){
  13.         return "I come from " + this._country;
  14.     },
  15.     set country(c){
  16.         this._country = c + c;
  17.     }
  18. }
  19.  
  20. // Call the object with some methods
  21. obj.stats();
  22. obj.value_of_this();
  23.  
  24. // Getters and setters share the same name as a key
  25. console.log(obj.country);
  26. obj.country = "Uruguay";
  27. console.log(obj.country);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement