VScode에서 클래스의 prototype을 콘솔에 찍었을 때 {}이 나오는 이유는?

Suxxzzy.log·2022년 1월 17일
0

에러핸들링

목록 보기
8/32

일단 console.log자체가 환경에 따라 다른 결과를 출력하는 함수이기 때문에 이것을 사용하면 안된다고 한다.(크롬 브라우저에서는 잘 찍힌다고 함. VScode는 안되는듯 )그리고 클래스 문법을 통해서 생성한 속성과 메소드는 기본적으로 열거가 불가능하다고 한다. 그래서 콘솔에 안 찍히는 거였음. (애초에 콘솔로그로 확인하는 것 자체가 잘못된것)

(not enumerable)

만약에, 환경과 상관없이 콘솔로그를 통해서 항상 prototype 객체를 출력하고 싶다면, 클래스 바깥에다가 다음과 같이 작성을 하는 것이 맞다.

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get area() {
    return this.calcArea()
  }

  calcArea() {
    return this.height * this.width;
  }
}
Polygon.prototype.test = function(){ return "test"}

// Note the different values for `enumerable`
//위의 것은 enumerable 속성이 false이고, 아래것은 enumerable 속성이 true이다.
console.log(Object.getOwnPropertyDescriptor(Polygon.prototype, 'calcArea'));
console.log(Object.getOwnPropertyDescriptor(Polygon.prototype, 'test'));

{
  "value": calcArea() {
    return this.height * this.width;
  },
  "writable": true,
  "enumerable": false,
  "configurable": true
}

{
  "value": function(){ return "test"},
  "writable": true,
  "enumerable": true,
  "configurable": true
}

reference
https://stackoverflow.com/questions/44186274/why-is-the-class-return-empty-prototype-object-first-time-in-javascript

profile
몫을 다하는 사람

0개의 댓글