继承

作者: 苏苡 | 来源:发表于2018-03-22 19:49 被阅读0次

继承方法:


1、继承第一种方式( 对象冒充 )

function Parent(username){

                this.username = username;

                this.hello =function(){

                                alert(this.username);

                }

}

function Child(username,password){

    通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承

     第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,

    第二步:执行this.method方法,即执行Parent所指向的对象函数

    第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法

                this.method =Parent;

                this.method(username);

                delete this.method;       ------------最关键的一行

                this.password = password;

                this.world =function(){

                                alert(this.password);

                }

}

var parent =new Parent("zhangsan");

var child =new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

2、继承第二种方式:call()方法方式

call方法是Function类中的方法

call方法的第一个参数的值赋值给类(即方法)中出现的this

call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

function test(str){  

                alert(this.name +" " + str);

}

var object =new Object();

object.name ="zhangsan";

test.call(object,"langsin");

此时,第一个参数值object传递给了test类(即方法)中出现的this,

而第二个参数"langsin"则赋值给了test类(即方法)的str

function Parent(username){

                this.username = username;

                this.hello =function(){

                                 alert(this.username);

                }

}

function Child(username,password){

                 Parent.call(this,username);

                 this.password = password;

                 this.world =function(){

                                alert(this.password);

                 }

}

var parent =new Parent("zhangsan");

var child =new Child("lisi","123456");

parent.hello();

child.hello();

child.world();

3、继承的第三种方式:apply()方法方式

apply方法接受2个参数,

A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this

B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数

function Parent(username){

                  this.username = username;

                  this.hello =function(){

                                  alert(this.username);

                   }

}

function Child(username,password){

                  Parent.apply(this,new Array(username));

                  this.password = password;

                  this.world =function(){

                                 alert(this.password);

                  }

}

var parent =new Parent("zhangsan");

var child =new Child("lisi","123456");

parent.hello();

child.hello();

4、继承的第四种方式:( 原型链方式 )

即子类通过prototype将所有在父类中通过prototype追加的属性和方法

都追加到Child,从而实现了继承

function Person(){  }

Person.prototype.hello ="hello";

Person.prototype . sayHello = function(){

              alert(this.hello);

}

function Child(){  }

Child.prototype =new Person();

这行的作用是:将Parent中将所有通过prototype追加的属性和方法

都追加到Child,从而实现了继承

Child.prototype.world ="world";

Child.prototype.sayWorld =function(){

                    alert(this.world);

}

var c =new Child();

c.sayHello();

c.sayWorld();

5、继承的第五种方式:( 混合方式 )

混合了call方式、原型链方式

function Parent(hello){

                 this.hello = hello;

}

Parent.prototype.sayHello =function(){

                 alert(this.hello);

}

function Child(hello,world){

                 Parent.call(this,hello);   ------------------将父类的属性继承过来

               this.world = world;   ---------------新增一些属性

}

Child.prototype =new Parent();

Child.prototype.sayWorld =function(){   ------------------ 将父类的方法继承过来

                      alert(this.world);   --------------------新增一些方法

}

var c =new Child("zhangsan","lisi");

c.sayHello();

c.sayWorld();

相关文章

  • 继承 继承

    属性拷贝 继承不单单能通过原型链实现,也能通过其他方式实现,属性拷贝就是其中一种方法。 通过属性拷贝也能实现继承子...

  • 继承(单继承,多继承)

    将共性的内容放在父类中,子类只需要关注自己特有的内容 python中所有的内容都是对象,所有的对象都直接或间接继承...

  • js继承方式

    类式继承 构造函数继承 组合继承 类式继承 + 构造函数继承 原型式继承 寄生式继承 寄生组合式继承 寄生式继承 ...

  • Python-学习之路-08 OOP -02

    单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类可以继承多个类 单继承的多继承的优缺点 菱形继承/钻石...

  • 原型相关(二)

    1.继承 继承方式:接口继承(只继承方法签名)实现继承(继承实际的方法)ECMAScript只支持实现继承,并且主...

  • 继承

    继承的引入和概述 继承案例和继承的好处 继承的弊端 Java中继承的特点 继承的注意实现和什么时候使用继承 继承中...

  • Java面向对象三大特性之继承

    继承 一、继承的特点 Java只支持单继承单继承 多继承 单继承、多继承优缺点①单继承优点:提高了代码的复用性,让...

  • 7、面向对象的程序设计3(《JS高级》笔记)

    三、继承 许多OO语言都支持两种继承方式:接口继承和实现继承。接口继承只继承方法签名,而实现继承则继承实际方法。由...

  • 【重学前端】JavaScript中的继承

    JavaScript中继承主要分为六种:类式继承(原型链继承)、构造函数继承、组合继承、原型式继承、寄生式继承、寄...

  • js之继承

    文章主讲 JS 继承,包括原型链继承、构造函数继承、组合继承、寄生组合继承、原型式继承、 ES6 继承,以及 多继...

网友评论

      本文标题:继承

      本文链接:https://www.haomeiwen.com/subject/creaqftx.html