Node.js
[Node.js] prototype 사용예시
hyun_ji
2023. 4. 26. 18:15
반응형
SMALL
1. 클래스 선언을 한다.
class User{}
2. 객체를 가져오는 방법은 아래의 양식이 된다.
const user = new User()
이 객체의 프로토타입은 User.prototype 이 된다.
3. 객체에 새로운 프로퍼티를 추가해줄 수 있다.
User.prototype.Hello = () => {
console.log('user')
}
Hello라는 메소드가 프로퍼티로써 추가되었다. 이 프로퍼티는 콘솔에 user 를 출력해준다.
4. user라는 이름으로 선언된 User 프로토타입 안에 있는 Hello 프로퍼티를 콘솔로 찍어본다.
console.log(user.Hello())
user가 출력될 것이다.
반응형
LIST