javascript 继承的几种方式

港控/mmm° 2024-02-17 15:15 177阅读 0赞
  1. //js继承有5种实现方式:
  2. /*1、继承第一种方式:对象冒充*/
  3. function Parent(username) {
  4. this.username = username;
  5. this.hello = function() {
  6. alert(this.username);
  7. }
  8. }
  9. function Child(username, password) {
  10. //通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承
  11. //第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,
  12. //第二步:执行this.method方法,即执行Parent所指向的对象函数
  13. //第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法
  14. this.method = Parent;
  15. this.method(username); //最关键的一行
  16. delete this.method;
  17. this.password = password;
  18. this.world = function() {
  19. alert(this.password);
  20. }
  21. }
  22. var parent = new Parent("zhangsan");
  23. var child = new Child("lisi", "123456");
  24. parent.hello();
  25. child.hello();
  26. child.world();
  27. /*2、继承第二种方式:call()方法方式
  28. call方法是Function类中的方法
  29. call方法的第一个参数的值赋值给类(即方法)中出现的this
  30. call方法的第二个参数开始依次赋值给类(即方法)所接受的参数*/
  31. function test(str) {
  32. alert(this.name + " " + str);
  33. }
  34. var object = new Object();
  35. object.name = "zhangsan";
  36. test.call(object, "langsin"); //此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str
  37. function Parent(username) {
  38. this.username = username;
  39. this.hello = function() {
  40. alert(this.username);
  41. }
  42. }
  43. function Child(username, password) {
  44. Parent.call(this, username);
  45. this.password = password;
  46. this.world = function() {
  47. alert(this.password);
  48. }
  49. }
  50. var parent = new Parent("zhangsan");
  51. var child = new Child("lisi", "123456");
  52. parent.hello();
  53. child.hello();
  54. child.world();
  55. /*3、继承的第三种方式:apply()方法方式
  56. apply方法接受2个参数,
  57. A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this
  58. B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数*/
  59. function Parent(username) {
  60. this.username = username;
  61. this.hello = function() {
  62. alert(this.username);
  63. }
  64. }
  65. function Child(username, password) {
  66. Parent.apply(this, new Array(username));
  67. this.password = password;
  68. this.world = function() {
  69. alert(this.password);
  70. }
  71. }
  72. var parent = new Parent("zhangsan");
  73. var child = new Child("lisi", "123456");
  74. parent.hello();
  75. child.hello();
  76. child.world();
  77. /*4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承*/
  78. function Person() {}
  79. Person.prototype.hello = "hello";
  80. Person.prototype.sayHello = function() {
  81. alert(this.hello);
  82. }
  83. function Child() {}
  84. Child.prototype = new Person(); //这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承
  85. Child.prototype.world = "world";
  86. Child.prototype.sayWorld = function() {
  87. alert(this.world);
  88. }
  89. var c = new Child();
  90. c.sayHello();
  91. c.sayWorld();
  92. /*5、继承的第五种方式:混合方式
  93. 混合了call方式、原型链方式*/
  94. function Parent(hello) {
  95. this.hello = hello;
  96. }
  97. Parent.prototype.sayHello = function() {
  98. alert(this.hello);
  99. }
  100. function Child(hello, world) {
  101. Parent.call(this, hello); //将父类的属性继承过来
  102. this.world = world; //新增一些属性
  103. }
  104. Child.prototype = new Parent(); //将父类的方法继承过来
  105. Child.prototype.sayWorld = function() { //新增一些方法
  106. alert(this.world);
  107. }
  108. var c = new Child("zhangsan", "lisi");
  109. c.sayHello();
  110. c.sayWorld();

发表评论

表情:
评论列表 (有 0 条评论,177人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JS继承方式

    1.原型继承 核心:将父类的实例作为子类的原型(并不是把父类中的属性和方法克隆一份一模一样的给子类,而是让子类父类之间增加了原型链接) 特点:父类中私有的和公有的都继

    相关 JS实现继承方式

    前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么如何在JS中实现继承呢?让我们拭目以待。 JS继承的实现方式 既然要实现继承,那么首先我们得有一