zoukankan      html  css  js  c++  java
  • 11JavaScript中通过prototype实现继承

     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>
  • 相关阅读:
    idea编辑器快捷键
    双随机系统遇到的简单样式问题
    HTTP笔记八
    HTTP笔记七
    HTTP笔记六
    HTTP笔记五
    HTTP笔记四
    HTTP笔记三
    HTTP笔记二
    HTTP笔记一
  • 原文地址:https://www.cnblogs.com/Forever-IT/p/5198945.html
Copyright © 2011-2022 走看看