IT개발/AJAX2009. 10. 28. 03:10

■ JSON 은 두가지 형태로 표현된다.
  • 이름 : 값   => {이름1: 값1, 이름2:값2, 이름3:값3}
  • 배열         => {값1, 값2, 값3}

경우에 따라서는 이름:값 / 배열의 형태를 혼합하여 사용할수 있다. 

var member = {
name: "최범균", 
favorateColors: ['파랑', '노랑', '빨강']
};

var message = member.name + member.favorateColors.length + member.favorateColors[0];


■ JSON 이용한 자바 스크립트 클래스 정의

클래스이름 = function(파라미터) {
     ------
}
클래스이름.prototype = {
함수명1: function(파라미터1) {
              ----
}
함수명2: function(파라미터2) {
 -----
}
}

예제)
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 + "]";
 }
}


Posted by 시티락