javascript中的继承

简介: 第一种方式: 对象冒充方式 可以实现多继承,但是不推荐使用这种方式。 因为当父类A中有方法sayHello,父类B中也有sayHello方法时,之类继承过程中两个父类的sayHello会产生覆盖。
  • 第一种方式: 对象冒充方式

可以实现多继承,但是不推荐使用这种方式。

因为当父类A中有方法sayHello,父类B中也有sayHello方法时,之类继承过程中两个父类的sayHello会产生覆盖。

 

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
//继承第一种方式:对象冒充 
//Child对象this,冒充Parent对象的this 
var Parent=function(username){ 
    this.username=username; 
    this.sayHello=function(){ 
        alert(this.username); 
    } 
}

var Child=function(username,password){ 
    //这三行代码是对象冒充的关键!! 
    this.method=Parent; 
    this.method(username); 
    delete this.method; 
    
    this.password=password; 
    this.sayWorld=function(){ 
        alert(this.password); 
    } 
}

var parent=new Parent("zhangsan");

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

//Parent只有一个方法 
parent.sayHello();

//Child有两个方法 
child.sayHello(); 
child.sayWorld();

</script> 
</head>         
<body> 
</body> 
</html>

 

  • 第二种方式: call方法方式

call方法是定义在Function对象中的方法,所以任意方法都可以调用call。

call方法的第1个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数的参数。

    • call方法
<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
//继承第一种方式:call方法方式 Function对象中的方法

function test(str1,str2){ 
    alert(this.name+" , "+str1+" , "+str2); 
} 
var object =new Object(); 
object.name="zhangsan";

test.call(object,"liujl","1234"); 
</script> 
</head>         
<body> 
</body> 
</html>
    • 使用call方法方式实现继承
<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
//继承第二种方式:使用call方法方式实现对象继承 
function Parent(username) 
{ 
    this.username=username; 
    this.sayHello=function(){ 
        alert(this.username); 
    } 
}

function Child(username,password) 
{ 
    Parent.call(this,username);//调用父对象的call方法

    //Parent.call(this,new Array(username)); 
    this.password=password; 
    this.sayWorld=function(){ 
        alert(this.password); 
    } 
}

var parent=new Parent("zhangsan"); 
var child=new Child("lisi","1234"); 
parent.sayHello();

child.sayHello(); 
child.sayWorld();

</script> 
</head>         
<body> 
</body> 
</html>

 

  • 第三种方式: apply方法方式

apply方法也属于 Function对象,所以方法都可以调用apply。

跟call方法唯一不同的是:只采用数组传递参数。而call接收离散参数列表也接收数组。

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
//继承第三种方式:使用apply方法方式实现对象继承 
function Parent(username) 
{ 
    this.username=username; 
    this.sayHello=function(){ 
        alert(this.username); 
    } 
}

function Child(username,password) 
{ 
    Parent.apply(this,new Array(username));//调用父对象的apply方法 
    this.password=password; 
    this.sayWorld=function(){ 
        alert(this.password); 
    } 
}

var parent=new Parent("zhangsan"); 
var child=new Child("lisi","1234"); 
parent.sayHello();

child.sayHello(); 
child.sayWorld();

</script> 
</head>         
<body> 
</body> 
</html>
  • 第四种方式: 原型链方式

缺点:无法给构造函数传递参数

<!DOCTYPE html>
<html> 
<head>
<script type="text/javascript">
//继承第四种方式:原型链方式(prototype chain)
function Parent(){
}
Parent.prototype.hello="hello";
Parent.prototype.sayHello=function(){
    alert(this.hello);
}

function Child(){

}
Child.prototype.world="world";
Child.prototype.sayWorld=function(){
    alert(this.world);
}

var child=new Child();
child.sayHello();
child.sayWorld();
</script>
</head>         
<body>
</body>
</html>

 

 

 

 

  • 第五种方式: 混合方式实现对象继承----推荐使用

1.父对象 ,将属性定义在构造函数里。

2.父对象 ,方法通过原型的方式定义在构造函数外。

3.子对象,通过call继承属性。

4.子对象,通过将自身的原型指向父对象来进行父对象方法的继承。

<!DOCTYPE html>
<html> 
<head>
<script type="text/javascript">
//继承第五种方式:混合方式

//1.父对象 ,将属性定义在构造函数里。
//2.父对象 ,方法通过原型的方式定义在构造函数外。
//3.子对象,通过call方法继承父对象属性。
//4.子对象,通过将自身的原型指向父对象来进行父对象方法的继承。

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 child=new Child("hello","world");
child.sayHello();
child.sayWorld();
</script>
</head>         
<body>
</body>
</html>

 

 

  • 继承综合练习

使用混合方式实现对象继承。

父对象Shape,让矩形Rectangle继承Shape。

<!DOCTYPE html>
<html> 
<head>
<script type="text/javascript">
//父对象--X形
function Shape(edge){
    this.edge=edge;
}
//父对象方法--获取面积
Shape.prototype.getArea=function(){
    return -1;
}


//子对象---矩形
function Rectangle(wide,height){
    //继承父对象的属性edge
    Shape.call(this,4);
    this.wide=wide;
    this.height=height;
}

Rectangle.prototype=new Shape();//继承父对象所有的方法
//重写某一个方法---获取矩形面积
Rectangle.prototype.getArea=function(){
    return this.wide*this.height;
}

//扩展一个方法
Rectangle.prototype.getEdge=function(){
    return this.edge;
}

var rectangle=new Rectangle(3,4);
alert(rectangle.getEdge());
alert(rectangle.getArea());

</script>
</head>         
<body>
</body>
</html>
开始做,坚持做,重复做
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript 继承的方式和优缺点
JavaScript 继承的方式和优缺点
13 0
|
JavaScript 前端开发 Java
深入JS面向对象(原型-继承)(三)
深入JS面向对象(原型-继承)
31 0
|
JavaScript 前端开发 Java
深入JS面向对象(原型-继承)(一)
深入JS面向对象(原型-继承)
32 0
|
2月前
|
JavaScript 前端开发
js开发:请解释原型继承和类继承的区别。
JavaScript中的原型继承和类继承用于共享对象属性和方法。原型继承利用原型链查找属性,节省内存但不支持私有成员。类继承通过ES6的class和extends实现,支持私有成员但占用更多内存。两者各有优势,适用于不同场景。
22 0
|
4月前
|
JavaScript
|
4月前
|
JavaScript 前端开发
原型继承在 JavaScript 中是如何工作
原型继承在 JavaScript 中是如何工作
21 0
|
4天前
|
JavaScript 前端开发
JavaScript 中最常用的继承方式
JavaScript中的继承有多种实现方式:1) 原型链继承利用原型查找,但属性共享可能引发问题;2) 借用构造函数避免共享,但方法复用不佳;3) 组合继承结合两者优点,是最常用的方式;4) ES6的class继承,是语法糖,仍基于原型链,提供更直观的面向对象编程体验。
8 1
|
4天前
|
设计模式 JavaScript 前端开发
在JavaScript中,继承是一个重要的概念
JavaScript继承有优点和缺点。优点包括代码复用、扩展性和层次结构清晰。缺点涉及深继承导致的复杂性、紧耦合、单一继承限制、隐藏父类方法以及可能的性能问题。在使用时需谨慎,并考虑其他设计模式。
8 2
|
4天前
|
JavaScript 前端开发 开发者
JavaScript 继承的方式和优缺点
JavaScript 继承的方式和优缺点
9 0
|
13天前
|
JavaScript 前端开发
JavaScript 继承的方式和优缺点
JavaScript 继承的方式和优缺点