Javascript如何实现继承?
时间: 2023-05-30 10:06:38 浏览: 85
Javascript实现继承的方式有:
1. 原型链继承:通过将子类的原型指向父类的实例来实现继承。
```
function Parent() {}
function Child() {}
Child.prototype = new Parent();
```
2. 构造函数继承:通过在子类中调用父类的构造函数,将父类的属性和方法复制到子类实例中来实现继承。
```
function Parent() {}
function Child() {
Parent.call(this);
}
```
3. 组合继承:结合原型链继承和构造函数继承的方式,既可以继承父类的属性和方法,又可以避免原型链继承中的问题。
```
function Parent() {}
function Child() {
Parent.call(this);
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
```
4. 寄生组合式继承:在组合继承的基础上进行优化,避免父类构造函数被调用两次。
```
function Parent() {}
function Child() {
Parent.call(this);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
```
相关问题
原型继承如何实现?
在 JavaScript 中,原型继承可以通过以下几种方式来实现:
1. 构造函数继承:通过在子类构造函数中调用父类构造函数来实现属性继承。在子类构造函数中调用父类构造函数可以使用 call 或 apply 方法实现。
```js
function Parent(name) {
this.name = name;
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
```
2. 原型链继承:通过将子类的原型对象指向父类的实例来实现原型继承。
```js
function Parent() {}
Parent.prototype.sayHello = function() {
console.log('Hello');
}
function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
```
3. 组合继承:通过将构造函数继承和原型链继承结合起来,既可以继承属性,也可以继承方法。
```js
function Parent(name) {
this.name = name;
}
Parent.prototype.sayHello = function() {
console.log('Hello');
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
```
4. 原型式继承:通过 Object.create 方法创建一个新对象,并将该对象的原型指向一个已有的对象,从而实现继承。
```js
var parent = {
name: 'parent',
sayHello: function() {
console.log('Hello');
}
};
var child = Object.create(parent);
child.age = 18;
```
5. 寄生式继承:通过在原型式继承的基础上,增强新对象,从而实现继承。
```js
function createChild(parent) {
var child = Object.create(parent);
child.sayName = function() {
console.log(this.name);
}
return child;
}
var parent = {
name: 'parent'
};
var child = createChild(parent);
child.sayName();
```
6. 寄生组合式继承:通过在组合继承的基础上,优化父类实例的创建,从而实现继承。
```js
function Parent(name) {
this.name = name;
}
Parent.prototype.sayHello = function() {
console.log('Hello');
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
```
在javaScript中实现继承方法
在 JavaScript 中,实现继承方法有以下几种方式:
1. 原型继承:利用原型链实现继承。通过将父类的实例作为子类的原型,子类就可以访问到父类的属性和方法。例如:
```javascript
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. 构造函数继承:利用 call 或 apply 方法将父类构造函数的作用域赋给子类。这种方式可以实现多继承。例如:
```javascript
function Parent(name) {
this.name = name;
}
function Child(name) {
Parent.call(this, name);
}
var child = new Child('child');
console.log(child.name); // 输出:child
```
3. 组合继承:结合原型继承和构造函数继承,既可以继承父类的属性和方法,又可以实现子类实例的独立。例如:
```javascript
function Parent(name) {
this.name = name;
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var child = new Child('child', 18);
console.log(child.name); // 输出:child
console.log(child.age); // 输出:18
child.sayName(); // 输出:child
```
4. 原型式继承:利用 Object.create() 方法创建一个新对象,以某个对象为原型,然后再对新对象进行修改。例如:
```javascript
var parent = {
name: 'parent',
sayName: function() {
console.log(this.name);
}
};
var child = Object.create(parent, {
name: {
value: 'child'
}
});
child.sayName(); // 输出:child
```
5. 寄生式继承:与原型式继承类似,但是在增强对象的方法时使用了一个函数封装。例如:
```javascript
var parent = {
name: 'parent',
sayName: function() {
console.log(this.name);
}
};
function createChild(original) {
var child = Object.create(original);
child.sayName = function() {
console.log('hello, ' + this.name);
}
return child;
}
var child = createChild(parent);
child.sayName(); // 输出:hello, parent
```
需要注意的是,在实现继承时应该注意避免出现属性和方法的重复定义,以及避免在父类的原型上修改引用类型的属性。