ES6 / TypeScript(2023-05-15)

권단비·2023년 5월 15일
0

IT

목록 보기
136/139

객체 리터럴 개선

      //2. 객체 리터럴 개선
      //객체 리터럴 개선(object literal enhancement)은 구조분해의 반대
      let funHike2 = {
        name: "백두산",
        elevation: 2744,
        print: function () {
          console.log(`${this.name}`);
        },
      };

      let name = "백두산";
      let elevation = 2744; //높이

      let print = function () {
        console.log(`${this.name}`);
      };

      let print = function () {
        console.log(`${this.name}`);
      };
      
      //this => 자바 : 객체 자기자신
      //this => 자바스크립트 : 호출한 놈..

      let funHike = { name, elevation, print };
      funHike.print();
      console.log(funHike); // // {name: "백두산", elevation : 2744}

this

// var x = window.document.write("메롱");
      var x = this.document.write("메롱"); // this는 window다.
      console.log(x); // window 출력

      function myFun() {
        return this;
      }
      console.log(myFun()); // this가 호출되어 window 출력

      //예시1=============================================================
      var num = 0; // 전역변수
      function addNum() {
        this.num = 100; // this : var num = 0에 100을 넣는다.
        num++; // window객체에 var num = 100이 들어가 있다.

        console.log(num); // 101
        console.log(window.num); // 101
        console.log(num === window.num); // true
      }
      addNum(); // window.addNum과 동일 | 앞에 ㅇㅇ.addNum이 붙지 않으면 무조건 window

0개의 댓글