javascript有几种继承方式
时间: 2024-01-07 11:09:59 浏览: 29
在JavaScript中,有以下几种继承方式:
1. 原型链继承:利用原型链实现继承。缺点是子类不能向父类构造函数传递参数。
2. 构造函数继承:在子类的构造函数中调用父类构造函数,并使用call或apply方法将父类的属性和方法添加到子类实例上。缺点是每次创建子类实例都会创建一份父类的属性和方法,浪费内存。
3. 组合继承:结合原型链继承和构造函数继承的优点,既可以继承父类的属性和方法,又可以向父类构造函数传递参数。
4. 原型式继承:借助Object.create方法实现继承。缺点是所有实例都会共享父类的属性和方法。
5. 寄生式继承:在原型式继承的基础上,增强了对象,返回新对象的同时还会增强对象。
6. 寄生组合式继承:结合组合继承和寄生式继承的优点,利用寄生式继承来继承父类的原型,然后将结果指定给子类的原型。
相关问题
JavaScript对象的几种继承方式及优缺点?
JavaScript中实现继承的方式有以下几种:
1. 原型链继承
优点:简单方便,易于理解和实现。
缺点:父类的引用属性会被多个实例共享,可能会出现意外修改;子类无法向父类构造函数传递参数。
示例代码:
```javascript
function Animal() {
this.species = 'animal';
}
function Cat() {}
Cat.prototype = new Animal();
```
2. 借用构造函数继承
优点:可以在子类构造函数中向父类构造函数传递参数;解决了父类引用属性被多个实例共享的问题。
缺点:无法实现函数的复用。
示例代码:
```javascript
function Animal(name) {
this.name = name;
}
function Cat(name) {
Animal.call(this, name);
}
```
3. 组合继承
优点:综合了原型链继承和借用构造函数继承的优点,既可以实现函数的复用,又可以向父类构造函数传递参数。
缺点:父类构造函数会被调用两次,造成了一些不必要的消耗。
示例代码:
```javascript
function Animal(name) {
this.name = name;
}
Animal.prototype.sayHello = function() {
console.log('Hello, my name is ' + this.name);
};
function Cat(name) {
Animal.call(this, name);
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
```
4. 原型式继承
优点:简单方便,可以在不必创建构造函数的情况下实现继承。
缺点:引用属性会被共享,可能会造成意外修改。
示例代码:
```javascript
function createObject(obj) {
function F() {}
F.prototype = obj;
return new F();
}
var animal = { species: 'animal' };
var cat = createObject(animal);
```
5. 寄生式继承
优点:可以在不必创建构造函数的情况下实现继承,可以为对象添加专门的方法。
缺点:引用属性会被共享,可能会造成意外修改。
示例代码:
```javascript
function createObject(obj) {
var clone = Object.create(obj);
clone.sayHello = function() {
console.log('Hello, my name is ' + this.name);
};
return clone;
}
var animal = { species: 'animal' };
var cat = createObject(animal);
```
总体来说,不同的继承方式各有优缺点,应根据具体的需求来选择合适的方式。
JavaScript继承方式
JavaScript有多种继承方式,以下是几种常见的方式:
1. 原型链继承:通过将父类实例赋值给子类的原型来实现继承。
```
function Parent() {
this.name = 'Parent';
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child() {}
Child.prototype = new Parent();
var child = new Child();
child.sayName(); // 输出 Parent
```
2. 构造函数继承:通过在子类构造函数中调用父类构造函数来实现继承。
```
function Parent() {
this.name = 'Parent';
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child() {
Parent.call(this);
}
var child = new Child();
child.sayName(); // 报错,因为子类没有继承父类的原型方法
```
3. 组合继承:结合原型链继承和构造函数继承的方式。
```
function Parent() {
this.name = 'Parent';
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child() {
Parent.call(this);
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child = new Child();
child.sayName(); // 输出 Parent
```
4. 寄生组合继承:对组合继承进行优化,避免了在子类原型上创建不必要的父类实例。
```
function Parent() {
this.name = 'Parent';
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child() {
Parent.call(this);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
var child = new Child();
child.sayName(); // 输出 Parent
```