본문 바로가기

개발하자/자바스크립트

객체생성하기

<html>
 <body>
 <script>
 function Car(c,d){
 this.color=c;
this.speed=d;
 }
 var car1=new Car("red",100);
 var car2=new Car("blue",200);

 document.write(car1.color+","+car1.speed+"<br>"); // red,100
 document.write(car2.color+","+car2.speed); // blue,200
 </script>
 </body>
</html>

 

 

 

<html>
<body>
<script>
function A(){
this.a=100;
this.b=function(){ return 200; }
this.c=f1;
}
function f1(){
return 300;
}
var o=new A();
document.write(o.a); // 100
document.write(o.b()); // 200
document.write(o.c()); // 300
</script>
</body>
</html>