Skip to content

Commit 002993a

Browse files
committed
EDIT: 6-2 내용 추가
1 parent 697cdc9 commit 002993a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

ch06/6-2.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,38 @@ var 어레이 = [4, 2, 1];
6161
프로토타입 체인에서 가장 먼저 찾은 함수가 호출되기 때문에, 인스턴스에서 정의한 함수가 호출된다.
6262

6363
클래스 기반 언어에서는 상속을 사용하지만, 자바스크립트와 같은 프로토타입 기반 언어에서는 객체를 원형(prototype)으로 삼고 이를 복제함으로써 상속과 비슷한 효과를 얻는다.
64+
65+
```js
66+
var 어레이 = [4, 2, 1];
67+
어레이(.__proto__).push(3);
68+
어레이(.__proto__)(.__proto__).hasOwnProperty(2); // true
69+
```
70+
71+
proto 는 생략할 수 있으므로 프로토타입 체이닝에 의해 배열 인스턴스에서도 Object의 prototype 메서드를 사용할 수 있다.
72+
73+
```js
74+
var Grade = function () {
75+
var args = Array.prototype.slice.call(arguments);
76+
for (var i = 0; i < args.length; i++) {
77+
this[i] = args[i];
78+
}
79+
this.length = args.length;
80+
};
81+
82+
var g = new Grade(100, 80);
83+
```
84+
85+
Grade의 인스턴스 g 는 유사배열객체이다.
86+
87+
g { 0: 100, 1: 80, length: 2 }
88+
89+
따라서 배열 메서드를 사용할 수 없다.
90+
91+
이를 가능하게 하고 싶다면, 인스턴스 g의 proto, 즉 Grade.prototype 이 배열의 인스턴스를 바라보게 해주면 된다.
92+
93+
```js
94+
Grade.prototype = [];
95+
96+
g.pop();
97+
g.push(90);
98+
```

0 commit comments

Comments
 (0)