Skip to content

Commit

Permalink
EDIT: 6-2 내용 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Leejha committed Jun 25, 2023
1 parent 697cdc9 commit 002993a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ch06/6-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,38 @@ var 어레이 = [4, 2, 1];
프로토타입 체인에서 가장 먼저 찾은 함수가 호출되기 때문에, 인스턴스에서 정의한 함수가 호출된다.

클래스 기반 언어에서는 상속을 사용하지만, 자바스크립트와 같은 프로토타입 기반 언어에서는 객체를 원형(prototype)으로 삼고 이를 복제함으로써 상속과 비슷한 효과를 얻는다.

```js
var 어레이 = [4, 2, 1];
어레이(.__proto__).push(3);
어레이(.__proto__)(.__proto__).hasOwnProperty(2); // true
```

proto 는 생략할 수 있으므로 프로토타입 체이닝에 의해 배열 인스턴스에서도 Object의 prototype 메서드를 사용할 수 있다.

```js
var Grade = function () {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
};

var g = new Grade(100, 80);
```

Grade의 인스턴스 g 는 유사배열객체이다.

g { 0: 100, 1: 80, length: 2 }

따라서 배열 메서드를 사용할 수 없다.

이를 가능하게 하고 싶다면, 인스턴스 g의 proto, 즉 Grade.prototype 이 배열의 인스턴스를 바라보게 해주면 된다.

```js
Grade.prototype = [];

g.pop();
g.push(90);
```

0 comments on commit 002993a

Please sign in to comment.