- prototype에는 두가지 종류가 있다.
- prototype link와 prototype property.
- prototype link 특징
- prototype link는 자신과 자신의 부모 Object의 연결링크로써 부모 Object의 property들을 가르키고 있다.
- 사용자가 어떤 instance 의 property를 호출하였을 때 그 property가 없으면 prototype link의 property (부모Object의 property)들을 살핀다.
- prototype link는 내부에서만 관리되고 실제 code에서 사용자가 직접 사용하지는 않는다. (못한다!)
- prototype link는 constructor property를 사용하여 어느 Object에 연결이 되어있는지 살펴볼 수 있다.
- prototype property 특징
- function Object의 instance를 만들었을 때만 가지고 있다.
- function Object에만 prototype.constructor가 존재함.
- 단순 object(ex: {})를 new를 사용하여 생성하면 prototype property가 없음
- Function Object의 경우 new로 instance 를 생성하면 prototype property가 상속되어 내려감
- new로 instance 를 생성하려면 prototype에 constructor (prototype.construtor)가 있어야함.
- 한마디로 함수 Object만 가능하다는 이야기임.
const { List, Set } = require('immutable')
function MyConstructor() {}
var myobject = new MyConstructor();
MyConstructor.prototype = {};
console.log([ myobject instanceof MyConstructor,
myobject.constructor == MyConstructor,
myobject.constructor == Object,
myobject instanceof Object ]); // [ false, true, false, true ]
const emptyList = new List();
console.log(emptyList.constructor); // { [Function: List] of: [Function], isList: [Function: isList] }
console.log(emptyList.prototype); // undefined
var o = {};
console.log(o.constructor); // [Function: Object]
console.log(o.prototype); // undefined
var a = [];
console.log(a.constructor); // [Function: Array]
console.log(a.prototype); // undefined
var n = new Number(3);
console.log(n.constructor); // [Function: Number]
console.log(n.prototype); // undefined
var c = function aaa() {};
console.log(c.constructor); // [Function: Function]
console.log(c.prototype); // aaa {}
var v = new c();
console.log(v.constructor); // [Function: aaa]
console.log(v.prototype); // undefined
var d = new n(); // TypeError: n is not a constructor!!!!
console.log(d.constructor);
console.log(d.prototype);
class AAA {
constructor() {
this.value = 10;
}
print(rValue){
console.log(this.value + rValue);
}
}
var a = new AAA();
console.log(a.constructor); // [Function: AAA]
console.log(a.prototype); // undefined
a.print(); // 10
//////////////////////////////////////////////////////
const { Record } = require('immutable')
const ABRecord = Record({ a: 1, b: 2 })
const myRecord = new ABRecord({ b: 3 })
console.log(ABRecord.constructor); // [Function: Function]
console.log(ABRecord.prototype);
// Record {
// constructor: [Function: Record],
// size: 2,
// _name: undefined,
// _keys: [ 'a', 'b' ],
// _defaultValues: { a: 1, b: 2 } }
console.log(myRecord.constructor); // [Function: Record]
console.log(myRecord.prototype); // undefined