'JSON 표기법을 통한 객체 접근방법 (자바스크립트 객체지향 코딩)'에 해당되는 글 1건

  1. 2009.12.02 JSON 표기법을 통한 객체 접근방법 (자바스크립트 객체지향 코딩)
반응형

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>javascript :prototype 사용 변수 선언 </title>
  <!--
  <script type="text/javascript" src="domTest2.js"></script>
  -->
 
  <SCRIPT LANGUAGE="JavaScript">
   
  <!--
   /*
   function Member(name,rename,age){
    this.name = name;
    this.rename = rename;
    this.age = age;
   }
   Member.prototype.toString = function(){
    return this.rename;
   }
   Member.prototype.getAge = function(){
    var today = new Date();
    var toage = parseInt(this.age.substring(0,2));


    return today.getFullYear() - toage;
   }
   
   Member.prototype.setValue = function(newName,rename,newage){
    this.name = newName;
    this.rename = rename;
    this.age = newage; 
   }
   
*/
   function log(text){
    var console = document.getElementById("debugConsole");
    if(console != null){
     console.innerHTML += text+"<br/>";
    }
   }
   
   Member = function(name,id,securityNo){
    this.name = name;
    this.id = id;
    this.securityNo = securityNo;
   }

   Member.prototype ={
    setValue : function(newName,newId,newSecurityNo){
     this.name = newName;
     this.id = newId;
     this.securityNo = newSecurityNo; 
    },
    getAge : function(){
     var birthYear = parseInt(this.securityNo.substring(0,2));
     var code = this.securityNo.substring(6,7);
     if(code == '1' || code == '2'){
      birthYear += 1900;
     }else if(code == '3' || code == '4'){
      birthYear += 2000;
     }
     var today = new Date();
     return today.getFullYear() - birthYear;
    },
    toString : function(){ 
     return this.name = "["+this.id+"]";
    }
   }
 
   window.onload = function(){

    var mem = new Member("hong","홍길동","7700001000000");
    log('변경전');
    log('회원이름 : '+mem.toString());
    log('회원연령 : '+mem.getAge());
    log('<br/>');

    mem.setValue("GilDong","길동","7000001000000");
    log('변경후');
    log('회원이름 : '+mem.toString());
    log('회원연령 : '+mem.getAge());

   }
  //-->
  </SCRIPT>
 </head>
 <body>
  <div id="debugConsole"></div>
 </body>
</html>

Posted by 1010