개발자의 길/Javascript
자바스크립트 프로토타입(Prototype)
모쿠
2017. 4. 6. 11:21
<자바스크립트 프로토타입>
- 생성자 함수의 프로토 타입을 변경할 수 있음
<21_prototype>
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 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript</title> </head> <body> <h1>생성자 함수 prototype 변경</h1> <div id="output"></div> <script type="text/javascript"> var output = document.getElementById('output'); // 생성자 함수 function Unit(name, money){ this.name = name; this.money = money; } // 생성자 함수의 proto type을 변경: Unit.prototype.gas = 0; Unit.prototype.attack = function() { output.innerHTML += this.name + '가 공격합니다!!' }; var unit1 = new Unit('drone', 50); for(x in unit1) { output.innerHTML += x + ' : ' + unit1[x] + '<br/>'; } unit1.attack(); var unit2 = new Unit('probe', 50); unit2.attack(); </script> </body> </html> | cs |
<출력화면>