chap95 / TS_study

JS 와 TS 스터디
1 stars 1 forks source link

4주차 - chap95 #14

Open chap95 opened 3 years ago

chap95 commented 3 years ago

4주차 문제입니다.


다음 코드와 관련된 3문제를 해결해주세요.

function Person(first, last, age, gender, interests, onBye) {
  this.first = first;
  this.last = last;
  this.age = age;
  this.gender = gender;
  this.interests = interests;
  this.onBye = onBye;

const person1 = new Person(
  "Bob",
  "Smith",
  32,
  "male",
  ["music", "skiing"],
  () => {
    console.log("bye bye!!");
  }
);
const person2 = new Person(
  "Tom",
  "Eden",
  30,
  "male",
  ["soccer", "game"],
  () => {
    console.log("bye bye!!");
  }
);
}
1. 위코드에서 다음 제시되는 코드를 실행했을 때 예측되는 결과 값을 말해주세요.
console.log(person1.__proto__);
console.log(Person.prototype);
console.log(person1.__proto__.constructor);
console.log(Person.constructor);
2. 다음 제시되는 코드의 메소드 생성방식과 위 코드에서의 메소드 생성방식의 차이점을 설명해주세요.
Person.prototype.onBye = function() {
  console.log('bye bye!!');
}
3. 2번 방식으로 생성된 메소드를 console.log 에 찍어내는 코드를 작성해주세요.
4. JS에서는 객체를 만드는 방법이 다양합니다. 각 방법은 근소한 차이가 존재하지만 공통점이 있습니다. 이 공통점이 어떤것인지 설명해주세요. (메소드 관점으로)
heozeop commented 3 years ago

1.

{
  first: "Bob"
  last: "Smith"
  age: 32
  gender: "male"
  interests: ["music", "skiing"]
  onBye: () => {
    console.log("bye bye!!");
  }
  constructor: function Person(...)
  __proto__
}
{
  first
  last
  age
  gender
  interests
  onBye
  constructor: function Person(...)
  __proto__
}
function Person(...)
function Person(...)
모르겠습니다 ㅎㅎㅎ...
  1. prototype에 사용하는 경우, 해당 메서드가 객체의 프로토타입으로 선언됩니다. 이를 상속한 객체에서 프로토타입을 통해 함수에 접근이 가능합니다.

  2. Person.prototype.onBye();

  3. Object의 메소드를 상속받습니다?