본문 바로가기
Web/Javascript

[Javascript 기본 개념] Javascript 문법-상속

by 나비와꽃기린 2016. 6. 23.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1.1  상속

Ex)

function Person(name){

this.name = name;

}

function Programmer(name){

this.name = name;

}

 

 

Person.prototype.name=null;

 

Person.prototype.introduce = function(){

return 'My name is '+this.name;

}

Programmer.prototype = new Person();

 

var p1 = new Programmer('egoing');

 

document.write(p1.introduce()+"<br />");

 

à Programmer이라는 생성자를 만들고, 그 생성자의 prototype Person의 객체를 연결했더니 Programmer 객체도 메소드 introduce를 사용할 수 있게 되는 것이다.


à Programmer Person의 기능을 상속하고 있는 것