객체를 정의하고 생성하는 동시에 사용되는 표기법
Object() 생성자 사용
객체 리터럴 활용 방법 (가독성)
생성자 함수를 이용하는 방법
var circle = new Circle(10);
document.write(circle.getArea()); //반지름 10 넓이는 314.1516 입니다.
function Circle(radius) {
this.radius = radius;
this.getArea = function() {
var area = Math.PI * Math.pow(this.radius, 2);
return "반지름 " + this.radius + "의 넓이는 " + area.toFixed(4) + "입니다.";
};
}
var circle = new Circle(10);
document.write(circle.getArea());
var 홍길동 = new Grade("홍길동",100,70,80);
document.write(홍길동.getAvg()); // 홍길동의 평균운 76.797 입니다.
document.write(홍길동.getGrade());// 홍길동의 성적운 미 입니다.
function Grade(name, kor, eng, math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
this.getAvg = function() {
var sum = this.kor + this.eng + this.math;
return sum / 3;
};
this.getGrade = function() {
var avg = this.getAvg();
if (avg >= 90) {
return '수';
} else if (avg >= 80) {
return '우';
} else if (avg >= 70) {
return '미';
} else if (avg >= 60) {
return '양';
} else {
return '가';
}
};
}
var 홍길동 = new Grade("홍길동",100,80,30);
document.write(홍길동.name + "의 평균은 " + 홍길동.getAvg() + " 입니다." + "<br>");
document.write(홍길동.name + "의 성적은 " + 홍길동.getGrade() + " 입니다." + "<br>");
설명:1부터 10까지 출력을 하되, 1초마다 증가된 숫자를 하나씩 찍고, 10초후 프로그램 완료를 찍을것
====================================================================
1
2
3
4
5
6
7
8
9
10
프로그램 완료
function number() {
var number = 1;
var intervalId = setInterval(function() {
console.log(number);
number++;
if (number > 10) {
clearInterval(intervalId);
console.log("프로그램 완료");
}
}, 1000); //(setInterval 500당 0.5초)
}
number();
Browser Object Model
브라우저 창과 관련된 기능을 제어하기 위한 객체 모델
Document Object Model
웹 페이지의 구조화된 문서를 표현하고 조작하기 위한 객체 모델
잘 봤습니다. 좋은 글 감사합니다.