티스토리 뷰

반응형
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<script type="text/javascript">
		/*
			protoytpe 기반 언어 객체 생성 과정
			빈 객체를 생성 > 변수, 함수 추가
		 */

		//빈 객체 생성
		var student = {};

		//변수 추가
		student.no = 1;
		student.name = "홍길동";
		student.grade = "2학년";
		student.score1 = 80;
		student.score2 = 90;

		//점수 수정 함수
		student.setScore = function(x, y) {
			/*
				객체 안에 포함된 메서드에서 다른 메서드를 호출하거나
				프로퍼티를 활용하고자 하는경우 this키워드를 이용
			*/
			this.score1 = x;
			this.score2 = y;
		}
		
		//점수 총합 함수
		student.sum = function() {
			var sum = this.score1 + this.score2;
			return sum;
		}
		
		//점수 평균 함수
		student.avg = function() {
			var language = this.score1;
			var math = this.score2;

			var avg = (language + math) / 2;
			return avg;
		}

		document.write("번호:" + student.no + 
				"<br>이름:" + student.name + 
				"<br>학년:" + student.grade + 
				"<br>언어:" + student.score1 + 
				"<br>수학:" + student.score2 + 
				"<br>총합:" + student.sum(student.score1, student.score2) +
				"<br>평균:" + student.avg() +
				"<br><br>변경된 점수");
		
		//점수를 변경합니다.
		student.setScore(50,70);
		document.write(
				"<br>언어:" + student.score1 + 
				"<br>수학:" + student.score2 + 
				"<br>총합:" + student.sum() +
				"<br>평균:" + student.avg());
	</script>
</body>
</html>



반응형
댓글
최근에 올라온 글
최근에 달린 댓글