
1)[[Prototype]]

const user = {
activate : true,
login : function(){
console.log('로그인 되었습니다.');
}
};
const student = {
passion : true
};
student.__proto__ = user;
console.log(student.activate);
student.login();
console.log(student.passion);
// => "student의 프로토타입은 user이다." 또는 "student는 user를 상속 받는다"
// 프로토타입 체인
const greedyStudent = {
class : 11,
__proto__ : student
};
console.log(greedyStudent.activate); //user에서 상속 받은 프로퍼티
console.log(greedyStudent.passion); //student에서 상속 받은 프로퍼티

2) prototype feature
const user = {
id : 'user',
login(){
console.log(`${this.id}님 로그인 되셨습니다`);
}
};
const student = {
__proto__ : user
};
student.id = 'user01';
student.login(); //user01님 로그인 되셨습니다.
for(let key in student){
// id는 student의 id, login은 user의 login()
console.log(key);
// key에 대응하는 프로퍼티가 상속 프로퍼티가 아니라 객체에 직접 구현 된 프로퍼티일 경우 true 반환
let isOwn = student.hasOwnProperty(key);
if(isOwn) console.log(`객체 자신의 property : ${key}`);
//객체 자신의 property : id
else console.log(`상속 property : ${key}`);
//상속 property : login
}
1) object constructor prototype

const user = {
activate : true,
login(){
console.log('로그인 되었습니다.');
}
};
function Student(name){
this.name = name;
}
// 여기서의 prototype은 앞에서 배운 프로토타입(__proto__)과 이름만 같을 뿐 실제로는 일반 프로퍼티이다.
Student.prototype = user;
// F.prototype은 new Function을 호출할 때만 사용된다.
// new F를 호출할 때 만들어지는 새로운 객체의 [[Prototype]]을 할당한다.
// student.__proto__ == user
let student = new Student('홍길동');
let student2 = new Student('유관순');
console.log(student.activate);
student2.login();
2) function prototype and conostructor property

function Student(){}
console.log(Student.prototype.constructor);
console.log(Student.prototype.constructor == Student);
let student = new Student(); //{ constructor : Student }를 상속 받음
console.log(student.constructor == Student);
1) object prototype
const obj = {};
console.log(obj.__proto__ === Object.prototype);
console.log(obj.toString === obj.__proto__.toString);
console.log(obj.toString === Object.prototype.toString);
2) built in object prototype
const num = new Number(100);
// num은 Number.prototype을 상속 받았는가?
console.log(num.__proto__ === Number.prototype);
// num은 Object.prototype을 상속 받았는가?
console.log(num.__proto__.__proto__ === Object.prototype);
// 체인 맨 위에는 null이 있다.
console.log(num.__proto__.__proto__.__proto__);
console.log(num);
// Number에도 toString()이 정의되어 있고, Object에도 toString()이 정의 되어 있다
// 중복 메서드가 있는 경우 체인에서 가까운 메서드가 사용된다.
// Number.prototype의 toString이 사용된다.
console.log(num.toString());
// 내장 프로토타입 수정 가능하나 되도록 변경하지 않는다.
// 명세서에 새로 등록 된 기능을 쓰고 싶지만 자바스크립트 엔진에 구현되어 있지 않은 경우 정도에만 변경한다.
String.prototype.hello = function(){
console.log(`hello, ${this}`);
};
"JavaScript".hello();
1) modern method
const user = {
activate : true
};
const student = Object.create(user);
console.log(student.activate);
console.log(Object.getPrototypeOf(student));
console.log(Object.getPrototypeOf(student) === user);
Object.setPrototypeOf(student, {});
console.log(Object.getPrototypeOf(student));
const obj = Object.create(user);
let key = "__proto__";
console.log(obj[key]); //obj.__proto__
obj[key] = {test : "새로운 객체 덮어쓰기"};
console.log(obj[key]);
console.log(obj.__proto__);