Length :
Prototype :
Constructor :
본 문서는 예제 9-4 소스코드를 바탕으로 HTML 문서 구조, DOM 요소를 통한 출력 방식, 그리고 JavaScript의 핵심 개념인 생성자 함수, prototype 속성 확장, constructor 참조 및 String 객체의 속성을 분석한 내용입니다.
<!DOCTYPE html>
<html>
<head>
<title>예제9-4String 속성</title>
</head>
<body>
<h3>String 속성</h3>
<p>Length : <b id="Lth"></b></p>
<p>Prototype : <b id="proto"></b></p>
<p>Constructor : <b id="cnst"></b></p>
<script>
function Human(name, job, birth) {
this.name = name;
this.job = job;
this.birth = birth;
}
Human.prototype.age = 23;
var kim = new Human("Kim", "student", 2003);
document.getElementById("proto").innerHTML = kim.age;
document.getElementById("cnst").innerHTML = kim.constructor;
document.getElementById("Lth").innerHTML = kim.name.length;
</script>
</body>
</html>
String 속성)을 정의합니다.id 속성(Lth, proto, cnst)을 통해 JavaScript로 접근하여 값을 동적으로 바인딩합니다.| 개념 요소 | 코드 표현 | 핵심 역할 및 동작 원리 |
|---|---|---|
| 생성자 함수 | function Human(...) |
유사한 구조의 객체를 생성하기 위한 설계도(템플릿) 역할 |
| 프로토타입 확장 | Human.prototype.age = 23; |
프로토타입 체인을 통해 모든 인스턴스가 공유하는 공통 속성 정의 |
| 생성자 참조 | kim.constructor |
인스턴스를 생성한 원본 생성자 함수(Human) 참조 반환 |
| String 속성 | kim.name.length |
문자열 원시 값이 래퍼 객체로 변환되어 길이를 반환 |
String 속성
Length : 3
Prototype : 23
Constructor : function Human(name, job, birth) { this.name = name; this.job = job; this.birth = birth; }