Web/Javascript
[Javascript 기본 개념] Javascript 문법-상속
나비와꽃기린
2016. 6. 23. 10:13
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의 기능을 상속하고 있는 것 |