소스1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>클로저를 이용한 bean 객체 생성</title> <script> function bean(a){ return{'getter':function(){ return a;}, 'setter':function(b){a=b;}}; } var obj = bean(1000); document.write("1 : "+obj.getter()+"<br/>"); obj.setter(500); document.write("2 : "+obj.getter()+"<br/>"); obj.setter(100); document.write("3 : "+obj.getter()); </script> </head> <body> </body> </html> | cs |
결과1
소스2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>클로저를 이용한 bean 객체 생성</title> <script> function student(num,kor,eng,math){ return{'getnum':function(){ return num;} ,'setnum':function(a){num=a;} ,'getkor':function(){ return kor;} ,'setkor':function(b){kor=b;} ,'geteng':function(){ return eng;} ,'seteng':function(c){eng=c;} ,'getmath':function(){ return math;} ,'setmath':function(d){math=d;} }; } var arr = new Array(); arr[0]=student(1111,80,90,80); arr[1]=student(2222,70,90,90); arr[2]=student(3333,75,64,60); arr[3]=student(4444,95,98,87); document.write("<table>"); for(var bean in arr){ document.write("<tr>"); document.write("<td>"); document.write(arr[bean].getnum()+" | "); document.write("</td>"); document.write("<td>"); document.write(arr[bean].getkor()); document.write("</td>"); document.write("<td>"); document.write(arr[bean].geteng()); document.write("</td>"); document.write("<td>"); document.write(arr[bean].getmath()); document.write("</td>"); document.write("</tr>"); } document.write("</table>"); </script> </head> <body> </body> </html> | cs |
결과2
소스3 (this를 이용한)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>클로저를 이용한 bean 객체 생성</title> <script> function student(){ return{'num':0,'kor':0,'eng':0,'math':0 ,'getnum':function(){ return this.num;} ,'setnum':function(a){this.num=a;} ,'getkor':function(){ return this.kor;} ,'setkor':function(b){this.kor=b;} ,'geteng':function(){ return this.eng;} ,'seteng':function(c){this.eng=c;} ,'getmath':function(){ return this.math;} ,'setmath':function(d){this.math=d;} }; } var arr = new Array(); arr[0]=student(); arr[0].setnum(1111); arr[0].setkor(90); arr[0].seteng(95); arr[0].setmath(85); arr[1]=student(); arr[1].setnum(2222); arr[1].setkor(78); arr[1].seteng(88); arr[1].setmath(95); document.write("<table>"); for(var bean in arr){ document.write("<tr>"); document.write("<td>"); document.write(arr[bean].getnum()); document.write("</td>"); document.write("<td>"); document.write(arr[bean].getkor()); document.write("</td>"); document.write("<td>"); document.write(arr[bean].geteng()); document.write("</td>"); document.write("<td>"); document.write(arr[bean].getmath()); document.write("</td>"); document.write("</tr>"); } document.write("</table>"); </script> </head> <body> </body> </html> | cs |
결과3
'* Programming > JavaScript' 카테고리의 다른 글
주소/우편번호 찾기 API (0) | 2019.10.24 |
---|---|
인쇄하기 (0) | 2018.03.08 |
THIS (0) | 2016.10.05 |
클로져(Closure) (0) | 2016.10.05 |
function(){} (함수) / 콜백함수 (0) | 2016.10.04 |