hoonnotes
객체지향 언어의 class와는 다르다
프로토타입으로 구현하선 상속을 조금 더 쉽게 해주는 도우미 역할!
function Circle(radius) {
this.radius = radius;
this.getArea = function () {
return Math.PI * this.radius ** 2;
};
}
const circle1 = new Circle(10);
const circle2 = new Circle(40);
Circle
Circle2
Circle1
radius
10
getArea
f ()
radius
40
getArea
f ()
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.getArea = function () {
return Math.PI * this.radius ** 2;
};
const circle1 = new Circle(10);
const circle2 = new Circle(40);
Circle
Circle2
Circle1
radius
10
radius
40
getArea
f ()
Circle.prototype
prototype
Text
getArea
getArea
생성자 함수
생성자함수.prototype
객체 인스턴스
prototype
constructor
state
method
__proto__
const person = { name: "hoon" };
const empty = {};
empty.__proto__ = person;
console.log(empty) // {}
console.log(empty.name) // hoon
const person = { name: "hoon" };
console.log(person.hasOwnProperty("__proto__")); // false
console.log(Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"));
// {enumerable: false, configurable: true, get: ƒ, set: ƒ}
console.log({}.__proto__ === Object.prototype); // true
const chicken = {};
const egg = {};
chicken.__proto__ = egg;
egg.__proto__ = chicken;
VM4414:5 Uncaught TypeError: Cyclic __proto__ value
at Object.set __proto__ [as __proto__] (<anonymous>)
at <anonymous>:5:15
chicken
egg
const chicken = {};
const egg = {};
chicken.__proto__ = egg;
egg.__proto__ = chicken;
VM4414:5 Uncaught TypeError: Cyclic __proto__ value
at Object.set __proto__ [as __proto__] (<anonymous>)
at <anonymous>:5:15
chicken
egg
const sample = Object.create(null)
console.log(sample.__proto__); // undefined
console.log(Object.getPrototypeOf(sample)); // null
const sample = Object.create(null)
console.log(sample.__proto__); // undefined
console.log(Object.getPrototypeOf(sample)); // null
const chicken = { sound: "꼬꼬" };
const egg = {};
console.log(Object.getPrototypeOf(egg));
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
Object.setPrototypeOf(egg, chicken);
console.log(egg.sound);
// 꼬꼬
(function () {}.hasOwnProperty("prototype")); // true
({}.hasOwnProperty("prototype")); // false
const Person = (name) => {
this.name = name;
};
console.log(Person.hasOwnProperty('prototype')); // false
console.log(Person.prototype); // undefined
const sample = {
method() {}
}
console.log(sample.method.hasOwnProperty('prototype')); // false
console.log(sample.method.prototype); // undefined
function Person(name) {
this.name = name;
}
const hoon = new Person("hoon");
console.log(Person.prototype === hoon.__proto__);
Person 생성자 함수
prototype
__proto__
Person.prototype
constructor
__proto__
Function.prototype
Object.prototype
hoon (인스턴스)
name
__proto__
hoon
new 키워드로 생성
prototype의 constructor 함수 실행
Person
function Person(name) {
this.name = name;
}
const hoon = new Person("hoon");
console.log(Person === hoon.constructor);
const obj = new Object();
console.log(obj.constructor === Object);
const func = new Function();
console.log(func.constructor === Function);
function Person() {}
const hoon = new Person();
console.log(hoon.constructor === Person);
const obj = {};
console.log(obj.constructor === Object);
const func = function () {};
console.log(func.constructor === Function);
const arr = [];
console.log(arr.constructor === Array);
const obj = new Object();
console.log(obj); // {}
class Sample extends Object {}
console.log(new Sample()); // Sample {}
const nums = new Object(1005);
console.log(nums); // [Number: 1005]
const strings = new Object("hoon");
console.log(strings); // [String: 'hoon']
Object 생성자 함수는,
생성한다!
const name = new String("hoon");
console.log(name.__proto__ === String.prototype);
const obj = new Object();
console.log(obj.__proto__ === Object.prototype);
const nums = new Object(123);
console.log(nums.__proto__ === Number.prototype);