Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2c8aba

Browse files
docs : update 3-4.js
1 parent 3b22b7c commit a2c8aba

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎생활코딩-3.객체지향/3-4-상속,prototype.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,49 @@
33
객체의 구조를 말그대로 상속, 물려받은 객체를 만들 수 있는 기능을 말한다.
44
만약 단순히 원래구조만 물려받는다면, 객체를 복사한 것과 다르지 않겠지만,
55
상속한 객체(자식객체라 한다)에서 상속받은 객체(부모객체라 한다)의 구조를 수정하고 변경해서 새로운 객체를 만들 수 있다.
6+
*/
7+
function Saram(name){ // 상속될 객체(부모객체, Saram)
8+
this.name = name
9+
this.age = 18
10+
}
11+
Saram.prototype.name = null // 상속될 프로퍼티를 프로토타입으로 정의
12+
Saram.prototype.introduce = function(){ // 상속될 메서드를 마찬가지로 정의
13+
return '내이름은 ' + this.name + '입니다.'
14+
}
615

716

17+
function Programmer(name, lang){ // 상속할 객체(자식객체, Programmer)
18+
this.name = name
19+
this.lang = lang
20+
}
21+
Programmer.prototype = new Saram() // 부모(Saram)의 프로퍼티, 메서드를 상속받음
22+
Programmer.prototype.lang = null // 자식 객체에서 프로퍼티를 추가
23+
Programmer.prototype.coding = function(){ // 자식 객체에서 메서드를 추가
24+
return "저는 " + this.lang + '으로 코딩합니다.'
25+
}
26+
27+
28+
// 테스트 코드
29+
var ps1 = new Saram('김숭')
30+
console.log(ps1.introduce())
31+
console.log(ps1.age, '살')
32+
var ps2 = new Programmer('김덩', 'Python')
33+
console.log(ps2.introduce())
34+
console.log(ps2.age, '살')
35+
console.log(ps2.coding())
36+
/*
37+
원래 객체의 프로퍼티, 메서드를 선언할 때,
38+
첫번째로, '객체명.프로퍼티 = 값' 같이 바깥에서 객체의 프로퍼티를 선언하거나,
39+
두번재로, 생성자 함수 안에 'this.프로퍼티 = 값' 같이 프로퍼티를 선언하기도 했다.
40+
그리고 이번에 배울 세번째는, 다른 상속받은 객체에서도 프로퍼티를 사용할 수 있게, '객체명.prototype.프로퍼티 = 값' 과 같이, 프로퍼티를 선언해줄 수 있다.
41+
(근데 이렇게 안하고 걍 다른방법으로 프로퍼티를 선언하고 정의해도, js에선 잘 상속받는 것 같다.)
42+
43+
이렇게 객체의 프로퍼티, 메서드를 선언, 정의하고, 상속받을 객체에서,
44+
'자식객체.prototype = new 부모객체()'와 같이 부모객체를 자식객체의 prototype 프로퍼티에 담으면, 부모객체의 프로퍼티, 메서드를 자식객체에서도 사용할 수 있게된다.
45+
*/
46+
47+
48+
/*
49+
그럼 prototype(프로포타입)은 뭘까?
50+
851
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /