Skip to content
- 교재 예제
- ex7-01.html
- ex7-02.html
- ex7-03.html
- ex7-04.html
- ex7-05.html
- ex7-06.html
- ex7-07.html
- ex7-08.html
- ex7-09.html
- ex7-10.html
- ex7-11.html
- 자바스크립트 객체 구성
- 프로퍼티(property)와 메소드(method)로 구성
- property : 속성(변수)
- method : 함수
- 객체 유형
- 코어 객체
- HTML DOM 객체
- 브라우저 객체
- 코어 객체
- 종류 : Array, Date, String, Math 등
- 생성 : let today = new Date();
- 사용 : document.write(today.toLocaleString());
- 배열
- 생성 : let cities = [“seoul”, “jeonju”, “gwangju”]; let a = new Array(5);
- 사용 : document.write(cities[1]); document.write(cities.length);
- 함수 : a.concat(cities); a.join(“-“); a.slice(2,4); a.sort(); a.reverse(); a.toString();
- Date 객체
- 생성 : let today = new Date(); let birth = new Data(2003, 5, 7); #2003년 6월 7일 (5->6번째 달)
- 사용 : let h = today.getHours(); .getFullYear(), .getMonth(), .getDate(), .Minutes(), .getSeconds(), .getMiliseconds(), .toLocaleString()
- String 객체
- 생성 : let h = new String(“Hello”); let h = “Hello”;
- 사용 : h.concat(“Java”); .length, .charAt(0), .indexOf(“e”), .slice(2,4), .substr(2,4), .toUpperCase(), .toLowerCase(), .replace(“l”, “r”), .trim()
- Math 객체
- 생성하지 않고 바로 사용
- 사용 : let sq = Math.sqrt(4);, Math.PI, Math.random(), Math.floor(3.14),
- 사용자 정의 객체 만들기 1 : new Object()로 만들기
- 객체 정의
- function inquiry() { return this.balance; }
- function deposit(money) { this.balance += money; }
- let account = new Object();
- account.owner = “홍길동”;
- account.balance = 35000;
- account.inquiry = inquiry;
- account.deposit = deposit;
- 객체 사용
- account.deposit(10000);
- account.withdraw(5000);
- 사용자 정의 객체 만들기 2 : 리터럴 표기법으로 만들기
- let account = {
owner = “홍길동”;
balance = 35000;
inquiry = function(){ return this.balance; }
deposit = function(money){ this.balance += money; }
}
- 사용자 정의 객체 만들기 3 : 객체의 틀(프로토타입)을 만들고 객체 생성하기
|
function Account(name, balance) { this.owner = name; this.balance = balance; this.inquiry = function () { return this.balance; } this.deposit = function (money) { this.balnace += money; } } let hong = new Account("홍길동", 100000) let song = new Account("송길동", 200000) Document.write(song.owner + song.balance) |
error: Content is protected !!