11JavaScript에서 prototype을 통한 상속

2539 단어
 1 <script type="text/javascript">
 2         function Person(name, age, gender) {
 3             this.userName = name;
 4             this.userAge = age;
 5             this.userGender = gender;
 6 
 7             this.sayHello = function () {
 8                 alert(' :' + this.userName + '  :' + this.userAge + ' 。 :' + this.userGender);
 9             };
10         }
11         // Person sayHi
12         Person.prototype.sayHi = function () {
13             alert('Person SayHi');
14         };
15 
16         var p = new Person(' ', 10, ' ');
17 
18         //Student (Student )
19         function Student(name, age,gender) {
20             this.userName = name;
21             this.userAge = age;
22             this.userGender = gender;
23         }
24 
25         // Student p 。
26         Student.prototype = p; 
27 
28 
29         Student.prototype.sayByebye = function () {
30             alert('bye bye!!!');
31         };
32 
33         var s = new Student(' ', 20,' ');
34 
35         s.sayHello();
36 
37 
38      
39 
40 
41     </script>

좋은 웹페이지 즐겨찾기