상속 : 객체의 로직을 그대로 물려받는 또 다른 객체를 만들 수 있는 기능을 의미한다. 기존의 로직을 수정하고 변경해서 파생된 새로운 객체를 만들 수 있게 해준다.
예시
//
function Person(name) {
 this.name=name;
 this.introduce=function (){
  return 'My name is '+this.name;
 }
}
var p1 = new Person('gildong');
document.write(p1.indroduce()+"<br />");

 

//
function Person(name) {
 this.name=name;
}
Person.prototype.name=null;
Person.prototype.introduce=function (){
 return 'My name is '+this.name;
}
var p1 = new Person('gildong');
document.write(p1.introduce()+"<br />");

 

//
function Person(name) {
 this.name=name;
}
Person.prototype.name=null;
Person.prototype.introduce=function (){
 return 'My name is '+this.name;
}

function Programmer(name){
 this.name=name;
}
Programmer.prototype = new Person();
var p1=new Programmer('gildong');
document.write(p1.introduce()+"<br />");

 

//
function Person(name){
    this.name = name;
}
Person.prototype.name=null;
Person.prototype.introduce = function(){
    return 'My name is '+this.name;
}
 
function Programmer(name){
    this.name = name;
}
Programmer.prototype = new Person();
Programmer.prototype.coding = function(){
    return "hello world";
}
 
var p1 = new Programmer('egoing');

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

 

prototype : 객체의 원형이다. prototype에 저장된 속성들은 생성자를 통해서 객체가 만들어질 때 그 객체에 연결된다. 객체와 객체를 연결하는 체인의 역할을 한다. 이러한 관계를 prototype chain이라 한다.
예제
//
function Ultra(){}
Ultra.prototype.ultraProp = true;
 
function Super(){}
Super.prototype = new Ultra();
 
function Sub(){}
Sub.prototype = new Super();
 
var o = new Sub();
console.log(o.ultraProp);

 

'Programming > Javascript' 카테고리의 다른 글

데이터 타입, 래퍼객체  (0) 2018.01.03
표준내장객체, Object 객체  (0) 2018.01.03
전역객체와 this  (0) 2018.01.03
생성자와 new  (0) 2018.01.03
함수의 호출  (0) 2018.01.03
블로그 이미지

꼴통보안인

,