首页 > 编程知识 正文

js继承实例讲解,如何实现继承js

时间:2023-12-29 20:32:12 阅读:331372 作者:BEDH

本文目录一览:

关于JS实现继承的方法都有哪一些?

定义一个父类:

// 定义一个动物类

function Animal (name) {

// 属性

this.name = name || ‘Animal’;

// 实例方法

this.sleep = function(){

console.log(this.name + ‘正在睡觉!’);

}

}

// 原型方法

Animal.prototype.eat = function(food) {

console.log(this.name + ‘正在吃:’ + food);

1.原型链继承

核心:将父类的实例作为子类的原型

function Cat(){

}

Cat.prototype = new Animal();

Cat.prototype.name = ‘cat’;

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.eat(‘fish’));

console.log(cat.sleep());

console.log(cat instanceof Animal); //true

console.log(cat instanceof Cat); //true

特点:

1.非常纯粹的继承关系,实例是子类的实例,也是父类的实例

2.父类新增的原型方法、属性,子类都能访问到

3.简单,易于实现

缺点:

1.要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行

(可以在cat构造函数中,为Cat实例增加实例属性)

2.无法实现多继承

3.来自原型对象的引用属性被所有实例共享

4.创建子类实例时,无法向父类构造函数传参

下面代码解释缺点3(注意是引用属性):

function Super(){

this.val = 1;

this.arr = [1];

}

function Sub(){

// ...

}

Sub.prototype = new Super(); // 核心

var sub1 = new Sub();

var sub2 = new Sub();

sub1.val = 2;

sub1.arr.push(2);

alert(sub1.val); // 2

alert(sub2.val); // 1

alert(sub1.arr); // 1, 2

alert(sub2.arr); // 1, 2

2.构造继承

核心:使用父类的构建函数来增强子类实例,等于复制父类的实例属性给子类(没用到原型),除了call方法,也可以用apply()

function Cat(name){

Animal.call(this);

this.name = name || ‘Tom’;

}

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.sleep());

console.log(cat instanceof Animal); // false

console.log(cat instanceof Cat); // true

特点:

1.解决了1中,子类实例共享父类引用属性的问题

2.创建子类实例时,可以向父类传递参数

3.可以实现多继承(call多个父类对象)

缺点:

1.实例并不是父类的实例,只是子类的实例

2.只能继承父类的实例属性和方法,不能继承原型属性和方法

3.无法实现函数复用,每个子类都有父类的实例函数的副本,影响性能

3.实例继承

核心:为父类实例添加新特性,作为子类实例返回

function Cat(name){

var instance = new Animal();

instance.name = name || ‘Tom’;

return instance;

}

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.sleep());

console.log(cat instanceof Animal); // true

console.log(cat instanceof Cat); // false

特点:

1.不限制调用方式,不管是new 子类()还是子类(),返回的对象都具有相同的效果

缺点:

1.实例是父类的实例,不是子类的实例

2.不支持多继承

4. 拷贝继承

核心:使用for…in将父类实例中的方法赋给子类实例

unction Cat(name){

var animal = new Animal();

for(var p in animal){

Cat.prototype[p] = animal[p];

}

Cat.prototype.name = name || ‘Tom’;

}

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.sleep());

console.log(cat instanceof Animal); // false

console.log(cat instanceof Cat); // true

特点:

1.支持多继承

缺点:

1.效率较低,内存占用高(因为要拷贝父类的属性)

2.无法获取父类不可枚举的方法(for in无法访问不可枚举的方法)

5.组合继承

核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用

function Cat(name){

Animal.call(this);

this.name = name || ‘Tom’;

}

Cat.prototype = new Animal();

//组合继承需要修复构造函数的指向

Cat.prototype.constructor=Cat;

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.sleep());

console.log(cat instanceof Animal); // true

console.log(cat instanceof Cat); // true

特点:

1.弥补了方式2的缺陷,可以继承实例属性、方法,也可以继承原型属性、方法

2.既是子类的实例,也是父类的实例

3.不存在引用属性的共享问题

4.可传参

5.函数可复用

缺点:

1.调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)

6.寄生组合继承

核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点

function Cat(name){

Animal.call(this);

this.name = name || ‘Tom’;

}

(function(){

// 创建一个没有实例方法的类

var Super = function(){};

Super.prototype = Animal.prototype;

//将实例作为子类的原型

Cat.prototype = new Super();

//寄生组合继承需要修复构造函数的指向

Cat.prototype.constructor=Cat;

})();

// Test Code

var cat = new Cat();

console.log(cat.name);

console.log(cat.sleep());

console.log(cat instanceof Animal); // true

console.log(cat instanceof Cat); //true

特点:

1.堪称完美

缺点:

1.实现较为复杂 (BY三人行慕课)

JS中关于继承的几种方式

1、原型链继承

2、构造继承

3、实例继承

4、拷贝继承

5、组合继承

6、寄生组合继承

js继承之组合继承

组合继承,指的是将原型链和借用构造函数的技术组合在一块,从而发挥二者之长的一种继承模式。

红宝书是这样说的,其思路是使用原型链实现对原型属性和方法的继承,通过借用构造函数来实现对实例属性的继承,这样,即通过在原型上定义方法实现了函数复用,又能保证每个实例都有自己的属性。

下面我们看一个组合继承的例子,没有女朋友我们就new一个女朋友

组合继承避免了原型链和借用构造函数的缺陷,融合了他们的优点,So,组合继承成为了JavaScript中最常用的继承模式。

js中继承的几种用法总结

一,js中对象继承

js中有三种继承方式

1.js原型(prototype)实现继承

复制代码 代码如下:

SPAN style="BACKGROUND-COLOR: #ffffff"SPAN style="FONT-SIZE: 18px"html

body

script type="text/javascript"

function Person(name,age){

this.name=name;

this.age=age;

}

Person.prototype.sayHello=function(){

alert("使用原型得到Name:"+this.name);

}

var per=new Person("马小倩",21);

per.sayHello(); //输出:使用原型得到Name:马小倩

function Student(){}

Student.prototype=new Person("洪如彤",21);

var stu=new Student();

Student.prototype.grade=5;

Student.prototype.intr=function(){

alert(this.grade);

}

stu.sayHello();//输出:使用原型得到Name:洪如彤

stu.intr();//输出:5

/script

/body

/html/SPAN/SPAN

2.构造函数实现继承

复制代码 代码如下:

SPAN style="FONT-SIZE: 18px"html

body

script type="text/javascript"

function Parent(name){

this.name=name;

this.sayParent=function(){

alert("Parent:"+this.name);

}

}

function Child(name,age){

this.tempMethod=Parent;

this.tempMethod(name);

this.age=age;

this.sayChild=function(){

alert("Child:"+this.name+"age:"+this.age);

}

}

var parent=new Parent("江剑臣");

parent.sayParent(); //输出:“Parent:江剑臣”

var child=new Child("李鸣",24); //输出:“Child:李鸣 age:24”

child.sayChild();

/script

/body

/html/SPAN

3.call , apply实现继承

复制代码 代码如下:

SPAN style="FONT-SIZE: 18px"html

body

script type="text/javascript"

function Person(name,age,love){

this.name=name;

this.age=age;

this.love=love;

this.say=function say(){

alert("姓名:"+name);

}

}

//call方式

function student(name,age){

Person.call(this,name,age);

}

//apply方式

function teacher(name,love){

Person.apply(this,[name,love]);

//Person.apply(this,arguments); //跟上句一样的效果,arguments

}

//call与aplly的异同:

//1,第一个参数this都一样,指当前对象

//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)

var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼”

per.say();

var stu=new student("曹玉",18);//输出:“曹玉”

stu.say();

var tea=new teacher("秦杰",16);//输出:“秦杰”

tea.say();

/script

/body

/html/SPAN

二、call和apply的用法(详细介绍)

js中call和apply都可以实现继承,唯一的一点参数不同,func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])。

JS手册中对call的解释:

复制代码 代码如下:

SPAN style="FONT-SIZE: 18px"call 方法

调用一个对象的一个方法,以另一个对象替换当前对象。

call([thisObj[,arg1[, arg2[, [,.argN]]]]])

参数

thisObj

可选项。将被用作当前对象的对象。

arg1, arg2, , argN

可选项。将被传递方法参数序列。

说明

call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。

如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。/SPAN

说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。下面以apply为例,说说这两个函数在 js中的重要作用。如:

复制代码 代码如下:

SPAN style="FONT-SIZE: 18px" function Person(name,age){ //定义一个类

this.name=name; //名字

this.age=age; //年龄

this.sayhello=function(){alert(this.name)};

}

function Print(){ //显示类的属性

this.funcName="Print";

this.show=function(){

var msg=[];

for(var key in this){

if(typeof(this[key])!="function"){

msg.push([key,":",this[key]].join(""));

}

}

alert(msg.join(" "));

};

}

function Student(name,age,grade,school){ //学生类

Person.apply(this,arguments);//比call优越的地方

Print.apply(this,arguments);

this.grade=grade; //年级

this.school=school; //学校

}

var p1=new Person("卜开化",80);

p1.sayhello();

var s1=new Student("白云飞",40,9,"岳麓书院");

s1.show();

s1.sayhello();

alert(s1.funcName);/SPAN

另外,Function.apply()在提升程序性能方面有着突出的作用:

我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。

比如

复制代码 代码如下:

SPAN style="FONT-SIZE: 18px"alert(Math.max(5,8)); //8

alert(Math.max(5,7,9,3,1,6)); //9

//但是在很多情况下,我们需要找出数组中最大的元素。

var arr=[5,7,9,1];

//alert(Math.max(arr)); // 这样却是不行的。NaN

//要这样写

function getMax(arr){

var arrLen=arr.length;

for(var i=0,ret=arr[0];iarrLen;i++){

ret=Math.max(ret,arr[i]);

}

return ret;

}

alert(getMax(arr)); //9

//换用apply,可以这样写

function getMax2(arr){

return Math.max.apply(null,arr);

}

alert(getMax2(arr)); //9

//两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。

//再比如数组的push方法。

var arr1=[1,3,4];

var arr2=[3,4,5];

//如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]

//arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]

//我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身)

var arrLen=arr2.length;

for(var i=0;iarrLen;i++){

arr1.push(arr2[i]);

}

//自从有了Apply,事情就变得如此简单

Array.prototype.push.apply(arr1,arr2); //现在arr1就是想要的结果/SPAN

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。