최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

클래스 생성의 기초
var MyClass = function($w,$h){
 this.width = $w;
 this.height = $h;
 this.name = "MyClass"
}

MyClass.prototype.build = function(){
 console.log(this.name); 
}

function init(){
 var _myClass = new MyClass(100,200);
 _myClass.build(); //MyClass
 console.log(_myClass.width); //100
 console.log(_myClass.height); //200
}

 

1 : MyClass 라는 클래스를 정의.

    생성자를 두개 만들었다.

7 : MyClass에 build 객체를 추가.

    여기서의 this는 MyClass이다.

12 : MyClass 인스턴스를 생성.

     출력값은 MyClass, 100, 200

 

 

호스트코드에서 호출하는 부분은

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>search</title>
<style type="text/css">
 * {padding:0;margin:0;}
</style>

<script src="js/jquery-1.7.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="js/test.js" charset="utf-8"></script>
</head>

<body>
<script>
 $(document).ready(function() {
  init();
 });
</script>
</body>
</html>
 
댓글