
다음 코드의 프로토타입이 생성되는 시점에 대해 설명해주세요
const Animal = (name) => {
this.name = name;
};
console.log(Animal.prototype);
다음 중 틀린 설명은?
다음 코드의 실행 결과에 대해 설명해주세요
const Vehicle = (function () {
function Vehicle(type) {
this.type = type;
}
Vehicle.prototype = {
constructor: Vehicle,
};
return Vehicle;
}());
const car = new Vehicle('Car');
const bike = new Vehicle('Bike');
console.log(car.constructor === Vehicle); // ?
console.log(bike.constructor === Vehicle); // ?
console.log(car.constructor === Object); // ?
console.log(bike.constructor === Object); // ?
다음 코드의 실행 결과에 대해 설명해주세요.
const car = {
drive() {
return "운전중...";
}
};
const electricCar = Object.create(car);
electricCar.charge = function() {
return "충전중...";
};
// 프로토타입 교체 (Object.setPrototypeOf 메서드)
// { fly() { return "나는중..."; } } 가 electircCar의 프로토타입
// 프로토타입 체인
// electricCar --> { fly() } --> Object.prototype --> null
Object.setPrototypeOf(electricCar, {
fly() {
return "나는중...";
}
});
console.log(electricCar.drive());
console.log(electricCar.charge());
console.log(electricCar.fly());
다음 코드의 실행 결과에 대해 설명해주세요.
const vehicle = {
type: "Car"
};
const myCar = Object.create(vehicle);
console.log(myCar instanceof Object);
console.log(myCar instanceof vehicle);
instanceof는 myCar의 프로토타입 체인을 따라가며 해당 객체가 Object의 인스턴스인지 확인한다.Object의 프로토타입 체인에 포함되므로 결과는 true .instanceof 의 오른쪽에는 반드시 생성자 함수가 와야 한다. 그러나 vehicle은 생성자 함수가 아니라 일반 객체.TypeError를 발생시킴.다음 설명 중 틀린 것은 무엇인가요?
4번다음 코드의 실행 결과를 예측하고, 그 이유를 설명하세요.
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function () {
return `Hello, my name is ${this.name}`;
};
const person1 = new Person("Alice");
console.log(person1.sayHello()); // (1)
console.log(person1.hasOwnProperty("sayHello")); // (2)
console.log("sayHello" in person1); // (3)
sayHello라는 프로토타입 메서드가 있기 때문에 Hello, my name is Alice가 출력된다.hasOwnProperty는 인수로 전달받은 프로퍼티 키가 객체 고유의 프로퍼티 키인 경우에만 true를 반환하고, 상속받은 프로토타입의 프로퍼티 키인 경우 false를 반환한다.false를 반환한다.true모든 함수는 기본적으로 Function.prototype을 상속받습니다. 그렇다면, 이로 인해 함수가 자동으로 사용할 수 있는 메서드에 대해 제시해주세요.
applycallbindtoStringlengthname모든 객체는 Object.prototype을 상속받나요? 만약 아니라면 어떤 경우인가요?
Object.create(null)로 생성된 객체
Object.create(null)은 프로토타입 체인을 가지지 않는 객체를 만듭니다.이런 객체는 Object.prototype을 상속받지 않는다.프로토타입 체인을 수동으로 조작한 경우
null로 설정하면, 해당 객체는 Object.prototype의 속성과 메서드를 상속받지 않는다.자바 스크립트가 지원하지 않는 패러다임은 무엇인가요 ?
4. 논리형 프로그래밍
틀린것을 고르세요
다음 중 자바스크립트의 instanceof 연산자를 사용할 때 TypeError가 발생하는 상황은 무엇인가요?
instanceof righttrue, 아니면 false