JSON 표기법으로 클래스 생성
출처:최범균의 Ajax [가메출판사]
<script>
Member = function(name, id, securityNo){ ////클래스 정의
this.name = name;
this.id = id;
this.securityNo = securityNo;
}
Member.prototype = {/////함수 정의
setValue:function(newName, newId, newSecurityNo){ ////setValue 함수 정의
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 + ']' + ' : ' + this.getAge();
}
}
//////---->클래스 호출..
var mem = new Member('우렁이','urung','7700001000000');
document.write('기존 : ' + mem.toString());
document.write('<br><br>');
mem.setValue('우렁이2', 'urung2', '0100004000000');
document.write('새 : ' + mem.toString());
</script>