본문 바로가기

Programming/HTML, CSS, Javascript

prototype을 사용한 예제

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script language="javascript">
var dki = {};
dki.math = function(a,b){
 this.a = a;
 this.b = b;
}
dki.math.prototype={
 mul:function(){
  return eval(this.a*this.b);
 },
 sum:function(){
  return eval(this.a+this.b);
 },
 sub:function(){
  return eval(this.a-this.b);
 }
}
dki.display = function(obj, key){
 this.obj = document.getElementById(obj);
 this.key = key;
}
dki.display.prototype={
 show:function(){
  this.obj.style.display="";
 },
 hide:function(){
  this.obj.style.display="none";
 }
}
</script>
</head>
<body>
<script language="javascript">
var cnt = new dki.math(7,8);
document.writeln("곱:"+cnt.mul());
document.writeln("<br>");
document.writeln("합:"+cnt.sum());
document.writeln("<br>");
document.writeln("차:"+cnt.sub());
</script>
<div id=aaa>
크르릉
</div>
<script language="javascript">
var dsp = new dki.display('aaa', 1);
</script>
<input type=button onclick="dsp.show()" value="보여주기">
<input type=button onclick="dsp.hide()" value="숨기기">
</body>
</html>