Child.prototype = Object.create(Parent.prototype);但是这行命令在使用过程中的速度不是会更慢吗,因为create在object的原型链上,这样导致使用这个方法需要沿着原型链去找到这个方法才能使用,这样也有资源浪费把
时间: 2024-03-30 15:40:05 浏览: 20
你提出的问题是合理的。确实,使用 `Object.create()` 方法会比直接调用 `Parent` 构造函数产生的效率稍低一些,因为它需要沿着原型链去找到 `Object.create()` 方法。
然而,这种效率差异非常微小,而且只会在创建对象时发生。一旦对象被创建,就不会再次访问 `Object.create()` 方法。因此,在大多数情况下,这种效率差异是可以忽略不计的。
另外,使用 `Object.create()` 方法的好处是,它可以避免在原型链上出现多余的属性,从而减少不必要的资源浪费和对象属性冗余。因此,如果我们希望代码更加简洁和可维护,那么使用 `Object.create()` 方法是一个更好的选择。
当然,如果你的应用程序对性能要求非常高,那么你可以使用其他更高效的方法来替代 `Object.create()` 方法。例如,你可以使用 ES6 的 `class` 关键字来定义类,它的效率与 `Object.create()` 方法相当,但语法更加简洁和易于理解。
相关问题
Child 函数重写了 Parent.prototype 对象,将其设置为一个新的 Parent 实例,为什么要设置为新的parent,不使用new命令设置不可以吗,这样不是会在他们的原型链上多一条空链吗
你提出的问题是非常好的,确实在这段代码中存在一些问题。
首先,我们要清楚 `Child.prototype = new Parent()` 这一行代码的作用是将 `Child` 的原型对象设置为一个新的 `Parent` 实例。这样做的目的是为了让 `Child` 实例可以继承 `Parent` 的属性和方法。但是这种做法有一个问题,就是会调用 `Parent` 构造函数来创建一个新的对象,然后将这个新对象赋值给 `Child.prototype`。这样一来,`Child.prototype` 对象上就会多出一些继承自 `Parent` 的属性,而这些属性是不需要的。因此,这种做法会导致一些不必要的资源浪费和对象属性冗余。
为了避免这种问题,我们可以使用 `Object.create()` 方法来创建一个空对象作为 `Child.prototype` 的原型对象,这样就不会出现多余的属性。具体的做法是将 `Child.prototype` 设置为 `Object.create(Parent.prototype)`,这样就可以将 `Child.prototype` 的原型对象设置为一个空对象,同时保证它能够继承 `Parent.prototype` 的属性和方法,而不会多出不必要的属性。
因此,正确的做法应该是这样的:
```javascript
function Parent(name) {
this.name = [name];
}
Parent.prototype.getFullName = function() {
let [name1, ...name2] = this.name.join(",").split("");
return name1 + "·" + name2;
};
function Child() {
Parent.call(this, "纳西妲");
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
let child1 = new Child();
let child2 = new Child();
```
这样就可以避免在原型链上出现多余的属性,同时保证 `Child` 实例能够正确地继承 `Parent` 的属性和方法。
子类中重写 this.method=method
可以通过在子类中重写 `this.method` 属性来实现对父类方法的重写。具体实现方法如下:
```
// 父类
function Parent() {}
Parent.prototype.method = function() {
console.log('Parent method');
}
// 子类
function Child() {}
// 继承父类
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
// 重写方法
Child.prototype.method = function() {
console.log('Child method');
}
// 创建子类实例
var child = new Child();
// 调用重写后的方法
child.method(); // 输出 "Child method"
```
在子类中重写 `this.method` 属性,可以覆盖父类中同名的方法实现,实现对父类方法的重写。同时,在子类中可以通过 `super` 关键字来调用父类中被覆盖的方法。例如:
```
// 子类
function Child() {}
// 继承父类
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
// 重写方法
Child.prototype.method = function() {
// 调用父类方法
Parent.prototype.method.call(this);
console.log('Child method');
}
// 创建子类实例
var child = new Child();
// 调用重写后的方法
child.method(); // 输出 "Parent method" 和 "Child method"
```
在子类的重写方法中使用 `Parent.prototype.method.call(this)` 调用父类方法,可以实现在子类中调用被覆盖的父类方法的效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)