HTML 문서 구조, DOM 요소를 통한 출력 방식, 그리고 JavaScript의 핵심 개념인 생성자 함수, prototype 속성 확장, constructor 참조 및 String 객체의 속성을 분석
<!DOCTYPE html> <!-- HTML5 문서 선언 -->
<html> <!-- HTML 문서 시작 -->
<head> <!-- 문서 정보 시작 -->
<title>예제9-4String 속성</title> <!-- 브라우저 제목 -->
</head> <!-- 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> // JavaScript 코드 시작
function Human(name, job, birth) { // Human 생성자 함수 정의
this.name = name; // 객체의 name 속성에 이름 저장
this.job = job; // 객체의 job 속성에 직업 저장
this.birth = birth; // 객체의 birth 속성에 출생연도 저장
} // 생성자 함수 종료
Human.prototype.age = 23; // prototype에 age 속성 추가
var kim = new Human("Kim", "student", 2003); // Human 객체 생성
document.getElementById("proto").innerHTML = kim.age;
// kim 객체의 age 속성값 출력
document.getElementById("cnst").innerHTML = kim.constructor;
// kim 객체의 생성자 함수 정보 출력
document.getElementById("Lth").innerHTML = kim.name.length;
// Kim 문자열의 길이 3 출력
</script> <!-- JavaScript 코드 종료 -->
</body> <!-- body 종료 -->
</html> <!-- HTML 문서 종료 -->
<!DOCTYPE html> <!-- HTML5 문서 선언 -->
<html> <!-- HTML 문서 시작 -->
<head> <!-- 문서 정보 시작 -->
<title>예제9-5String 메소드1</title> <!-- 브라우저 제목 -->
</head> <!-- head 종료 -->
<body> <!-- 화면 내용 시작 -->
<h3>String 메소드1</h3> <!-- 실습 제목 -->
<p>charAt : <b id="ca"></b></p> <!-- charAt 결과 출력 위치 -->
<p>charCodeAt : <b id="cca"></b></p> <!-- charCodeAt 결과 출력 위치 -->
<p>concat : <b id="cnct"></b></p> <!-- concat 결과 출력 위치 -->
<p>indexOf : <b id="ido"></b></p> <!-- indexOf 결과 출력 위치 -->
<p>lastIndexOf : <b id="lido"></b></p> <!-- lastIndexOf 결과 출력 위치 -->
<p>replace : <b id="rpl"></b></p> <!-- replace 결과 출력 위치 -->
<script> // JavaScript 코드 시작
var str = "hello, welcome to AI Software!!"; // 실습 문자열 저장
var str2 ="???"; // 연결할 두 번째 문자열 저장
var ca = str.charAt(0); // 0번 위치의 문자 h 반환
var cca = str.charCodeAt(0); // 0번 위치 문자 h의 유니코드 값 반환
var cnct = str.concat(str2); // 두 문자열 연결
var ido = str.indexOf("AI"); // AI가 처음 나타나는 위치 반환
var lido = str.lastIndexOf("to"), rpl = str.replace("hello","Good Bye");
// lastIndexOf("to")는 to가 마지막으로 나타나는 위치 반환
// replace()는 hello를 Good Bye로 교체
document.getElementById("ca").innerHTML = ca;
// charAt 실행 결과 출력
document.getElementById("cca").innerHTML = cca;
// charCodeAt 실행 결과 출력
document.getElementById("cnct").innerHTML = cnct;
// concat 실행 결과 출력
document.getElementById("ido").innerHTML = ido;
// indexOf 실행 결과 출력
document.getElementById("lido").innerHTML = lido;
// lastIndexOf 실행 결과 출력
document.getElementById("rpl").innerHTML = rpl;
// replace 실행 결과 출력
</script> <!-- JavaScript 코드 종료 -->
</body> <!-- body 종료 -->
</html> <!-- HTML 문서 종료 -->
<!DOCTYPE html> <!-- HTML5 문서 선언 -->
<html> <!-- HTML 문서 시작 -->
<head> <!-- 문서 정보 시작 -->
<title>예제9-6String 메소드2</title> <!-- 브라우저 제목 -->
</head> <!-- head 종료 -->
<body> <!-- 화면 내용 시작 -->
<h3>String 메소드2</h3> <!-- 실습 제목 -->
<p>match : <b id="ca"></b></p> <!-- match 결과 출력 위치 -->
<p>search : <b id="cca"></b></p> <!-- search 결과 출력 위치 -->
<p>slice : <b id="cnct"></b></p> <!-- slice 결과 출력 위치 -->
<p>split : <b id="ido"></b></p> <!-- split 결과 출력 위치 -->
<p>toUpperCase : <b id="lido"></b></p> <!-- 대문자 변환 결과 출력 위치 -->
<p>toString : <b id="rpl"></b></p> <!-- 문자열 변환 결과 출력 위치 -->
<script> // JavaScript 코드 시작
var str = "Hello, welcome, AI, Software"; // 실습 문자열 저장
var str2 = 5678; // 숫자형 데이터 저장
var ca = str.match("AI"), cca = str.search("Software");
// match()는 AI와 일치하는 문자열 검색
// search()는 Software가 시작되는 위치 반환
var cnct = str.slice(7,12), ido = str.split(" ");
// slice()는 7번부터 11번 인덱스까지 추출
// split()은 공백을 기준으로 문자열 분리
var lido = str.toUpperCase(), rpl = str2.toString();
// toUpperCase()는 영문자를 대문자로 변환
// toString()은 숫자 5678을 문자열로 변환
document.getElementById("ca").innerHTML = ca;
// match 실행 결과 출력
document.getElementById("cca").innerHTML = cca;
// search 실행 결과 출력
document.getElementById("cnct").innerHTML = cnct;
// slice 실행 결과 출력
document.getElementById("ido").innerHTML = ido;
// split 실행 결과 출력
document.getElementById("lido").innerHTML = lido;
// toUpperCase 실행 결과 출력
document.getElementById("rpl").innerHTML = rpl;
// toString 실행 결과 출력
</script> <!-- JavaScript 코드 종료 -->
</body> <!-- body 종료 -->
</html> <!-- HTML 문서 종료 -->
String 속성)을 정의합니다.id 속성(Lth, proto, cnst)을 통해 JavaScript로 접근하여 값을 동적으로 바인딩합니다.| 개념 요소 | 코드 표현 | 핵심 역할 및 동작 원리 |
|---|---|---|
| 생성자 함수 | function Human(...) |
유사한 구조의 객체를 생성하기 위한 설계도(템플릿) 역할 |
| 프로토타입 확장 | Human.prototype.age = 23; |
프로토타입 체인을 통해 모든 인스턴스가 공유하는 공통 속성 정의 |
| 생성자 참조 | kim.constructor |
인스턴스를 생성한 원본 생성자 함수(Human) 참조 반환 |
| String 속성 | kim.name.length |
문자열 원시 값이 래퍼 객체로 변환되어 길이를 반환 |
| 메서드명 | 사용 예시 | 동작 설명 및 결과 |
|---|---|---|
charAt(index) |
str.charAt(0) |
특정 인덱스(0번째)에 위치한 문자 추출 ➔ h |
charCodeAt(index) |
str.charCodeAt(0) |
특정 인덱스 문자의 유니코드(Unicode) 값 반환 ➔ 104 ('h'의 ASCII/Unicode 값) |
concat(string) |
str.concat(str2) |
문자열을 서로 결합 ➔ hello, welcome to AI Software!!??? |
indexOf(substring) |
str.indexOf("AI") |
지정한 문자열이 처음으로 등장하는 위치(인덱스) 검색 ➔ 17 |
lastIndexOf(substring) |
str.lastIndexOf("to") |
지정한 문자열이 마지막으로 등장하는 위치 검색 ➔ 14 |
replace(target, replacement) |
str.replace("hello", "Good Bye") |
특정 문자열을 찾아 다른 문자열로 치환 ➔ Good Bye, welcome to AI Software!! |
| 메서드명 | 사용 예시 | 동작 설명 및 결과 |
|---|---|---|
match(pattern) |
str.match("AI") |
지정한 정규식 또는 문자열과 일치하는 항목을 검색하여 배열로 반환합니다. ➔ AI |
search(pattern) |
str.search("Software") |
지정한 정규식 또는 문자열이 처음으로 시작되는 인덱스 위치를 반환합니다. ➔ 20 |
slice(start, end) |
str.slice(7, 12) |
인덱스 7부터 12 직전(11)까지의 문자열을 부분 추출합니다. ➔ welco |
split(separator) |
str.split(" ") |
구분자(공백 " ")를 기준으로 문자열을 분할하여 배열을 생성합니다. (DOM 출력 시 콤마로 연결됨) ➔ Hello,,welcome,,AI,,Software |
toUpperCase() |
str.toUpperCase() |
문자열 내부의 모든 영문 소문자를 대문자로 변환합니다. ➔ HELLO, WELCOME, AI, SOFTWARE |
toString() |
str2.toString() |
숫자 객체/원시 값(5678)을 문자열 형태("5678")로 변환합니다. ➔ 5678 |
String 속성
Length : 3
Prototype : 23
Constructor : function Human(name, job, birth) { this.name = name; this.job = job; this.birth = birth; }