본문 바로가기

개발하자/자바스크립트

원하는 객체의 값 변경(강아지 이름 변경 예제)

<html>
 <body>
 <script>
function Dog(a,b,c){
 this.name=a;
 this.breed=b;
 this.age=c;
this.setName=function(cn){this.name=cn};    //새로운 값 셋팅
this.getName=function(){return this.name};    //새로운 값 리턴
 }


 var dog1=new Dog("뚱돌이","페키니즈",10);
 var dog2=new Dog("똘망이","씨추",5);

 document.write(dog1.name+","+dog1.breed+","+dog1.age);
 document.write("<br>");
 document.write(dog2.name+","+dog2.breed+","+dog2.age);
 dog1.setName("뚱이");
document.write(dog1.getName()); // 뚱이
</script>
 </body>
</html>