프로토타입 객체(The Prototype Object)

2023. 5. 9. 12:48프론트엔드/기술 면접 (JavaScript Interview Questions)

반응형

The prototype object is simply an object that multiple other objects can refer to to get any information or functionality that they need. For our needs, each of our constructor functions will have a prototype that all their instances will be able to refer to.

프로토타입 객체는 필요한 정보나 기능을 얻기 위해 다른 여러 객체가 참조할 수 있는 객체입니다. 필요에 따라 각 생성자 함수에는 모든 인스턴스가 참조할 수 있는 프로토타입이 있습니다.

 

So let's clarify what this means by doing a couple of examples. If we take our User constructor function we previously made, we can put properties onto its prototype that all of our User instances like user1 and user2 will have access to and be able to use.

몇 가지 예를 통해 이것이 무엇을 의미하는지 확인 해봅시다. 이전에 만든 User 생성자 함수를 사용해보면, user1user2와 같은 모든 User 인스턴스가 액세스할 수 있고 사용할 수 있는 프로토타입에 속성을 넣을 수 있습니다.

 

function User(firstName, lastName, age, gender) {
	this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.gender = gender;
}

const user1 = new User('Jill', 'Smith', 33, 'female');
const user2 = new User('Bob', 'Smith', 24, 'male');

User.prototype.emailDomain = '@gmail.com';

> user1.emailDomain
> "@gmail.com"

User.prototype.getEmailAddress = function() {
	return this.firstName + this.lastName + this.emailDomain;
}

> user1.getEmailAddress();
> "JillSmith@gmail.com"

 

반응형