• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Object] JS - Object Prototypes - 객체프로토타입 (= 객체원형) ★

모든 JS 객체는 프로토 타입에서 속성과 메서드를 상속.

 

 

객체 생성자

이전 시간에 배운 내용 복습

 

<p id="demo"></p>


<script>

function Site(nameValue, hostValue, openValue) {

  this.name = nameValue;

  this.host = hostValue;

  this.open = openValue;

}


var hz  = new Site("홈짱닷컴", "Homzzang.com", 2012);

var sir = new Site("그누보드", "Sir.kr", 2001);


var hz_site = hz.name + " " + hz.host + " " + hz.open;

var sir_site = sir.name + " " + sir.host + " " + sir.open;

document.getElementById("demo").innerHTML = hz_site + " / " + sir_site; 

</script>

 

결과보기 : 홈짱닷컴 Homzzang.com 2012 / 그누보드 Sir.kr 2001


 

※ 기존 객체에 새 속성 추가하는 방식으로는 객체 생성자에 새 속성 추가 못함.

 

<p id="demo"></p>


<script>

function Site(nameValue, hostValue, openValue) {

  this.name = nameValue;

  this.host = hostValue;

  this.open = openValue;

}


Site.code = "HTML CSS JS JQ PHP SQL";

  

var hz  = new Site("홈짱닷컴", "Homzzang.com", 2012);

var sir = new Site("그누보드", "Sir.kr", 2001);

  

document.getElementById("demo").innerHTML = hz.code;

</script>

 

결과보기 : undefined


 

※ 생성자에 새 속성 추가하려면 생성자 함수 안에 추가해야 함.

※ 이 방법으로, 객체 속성이 기본값 갖을 수 있음. 

 

<p id="demo"></p>


<script>

function Site(nameValue, hostValue, openValue) {

  this.name = nameValue;

  this.host = hostValue;

  this.open = openValue;

  this.code = "HTML CSS JS JQ PHP SQL";

}

 

var hz  = new Site("홈짱닷컴", "Homzzang.com", 2012);

var sir = new Site("그누보드", "Sir.kr", 2001);

  

document.getElementById("demo").innerHTML = hz.code;

</script>

 

결과보기: HTML CSS JS JQ PHP SQL

 

 

프로토 타입 상속

 

1.

모든 JS 객체는 프로토 타입에서 속성과 메서드를 상속.

(예)

Date 객체는 Date.prototype에서 상속.

Array 객체는 Array.prototype에서 상속

Site 객체는 Site.prototype에서 상속

 

2.

Object.prototype은 프로토 타입 상속 체인의 맨 위에 있음.

Date 객체, Array 객체, Site 객체는 Object.prototype에서 상속됨.

 

 

 

객체에 속성과 메소드 추가 


객체에 새로운 속성 (또는 메소드) 추가 경우 : 앞장에서 배운대로 하면 됨.

객체 생성자에 새로운 속성 (또는 메서드) 추가 경우 : 지금 배우는 prototype 속성 이용하면 편함.

 

 

 

prototype 속성 - 객체 생성자에 속성・메서드 추가

 

※ JS prototype 속성 사용하면 객체 생성자에 새로운 속성 추가 가능.

 

<p id="demo"></p>


<script>

function Site(nameValue, hostValue, openValue) {

  this.name = nameValue;

  this.host = hostValue;

  this.open = openValue;

}


Site.prototype.code = "HTML CSS JS JQ PHP SQL";

var hz = new Site("홈짱닷컴", "Homzzang.com", 2012);

 

document.getElementById("demo").innerHTML = hz.code

</script>

 

결과보기 : HTML CSS JS JQ PHP SQL


 

※ JS prototype 속성 사용하면 객체 생성자에 새로운 메서드 추가 가능.

 

<p id="demo"></p>


<script>

function Site(nameValue, hostValue, openValue) {

  this.name = nameValue;

  this.host = hostValue;

  this.open = openValue;

}


Site.prototype.intro = function() {

  return this.name + " " + this.host

};


var hz = new Site("홈짱닷컴", "Homzzang.com", 2012);


document.getElementById("demo").innerHTML = hz.intro()

</script>

 

결과보기: 홈짱닷컴 Homzzang.com


 

★ 자신만의 프로토 타입만 수정. 표준 JS 객체의 프로토 타입은 수정 금지.

 



분류 제목
DOM_Element JS - isDefaultNamespace() 메서드 - 네임스페이스가 기본값인지 체크
DOM_Element JS - isEqualNode() 메서드 - 노드 동일여부 비교 (IE9 이상)
DOM_Element JS - isSameNode() 메서드 - 노드 동일여부 비교 (IE9 이상. cf. Firefox 지원X)
DOM_Element JS - isSupported() 메서드 - 지정기능이 지정노드에 지원되는지 확인 (※ 사용 비권장)
DOM_Element JS - lang 속성 - 요소의 lang 속성값 설정/반환
DOM_Element JS - lastChild 속성 ★ = 마지막자식노드 (= lastChild속성 = 라스트차일드 속성)
DOM_Element JS - lastElementChild 속성 - 마지막 자식요소 (IE9 이상)
DOM_Element JS - namespaceURI 속성 - 네임스페이스URI (IE9 이상)
DOM_Element JS - nextSibling 속성 - 바로다음 형제노드
DOM_Element JS - nextElementSibling 속성 - 바로다음 형제요소 (IE9 이상)
DOM_Element JS - nodeName 속성 - 노드명 (= 노드이름)
DOM_Element JS - nodeType 속성 ★ - 노드타입 반환 (읽기전용) ※ 노드유형 = 노트형식 = 노드종류
DOM_Element JS - nodeValue 속성 - 지정노드의 노드값 설정/반환 (= nodeValue속성 = 노드밸류속성)
DOM_Element JS - normalize() 메서드 - 공백제거 후, 인접텍스트노드 합치기 (Element경우)
DOM_Element JS - offsetHeight 속성 - 가시높이 (= 요소실제높이 = 요소높이 height+ + paddi…
DOM_Element JS - offsetWidth 속성 ★ = 가시너비 (= 요소실제너비 = 요소너비 width + paddin…
DOM_Element JS - offsetLeft 속성 - 가시좌측위치 (= 가시왼쪽위치) (IE8 이상)
DOM_Element JS - offsetParent 속성 - static 이외의 position 갖는 최근접조상요소 (= 가장가…
DOM_Element JS - offsetTop 속성 - 가시 상단 위치 (IE8 이상)
DOM_Element JS - ownerDocument 속성 - 노드의 소유자문서를 HTMLDocument 객체로 반환
29/67
목록
찾아주셔서 감사합니다. Since 2012