JS中构造函数继承问题注意事项总结

在 JavaScript 中,继承是通过原型链来实现的。当你想要创建一个子类(比如 Student)继承一个父类(比如 Person)时,通常会使用 Object.create 来创建 Student 的原型对象。这背后有一些重要的原因:

1. 共享与独立性

当你执行 Student.prototype = Person.prototype 时,Student 的原型对象与 Person 的原型对象指向同一个对象。这意味着对 Student.prototype 的任何修改都会直接影响到 Person.prototype,反之亦然。这种共享可能会导致意外的副作用。

javascript 复制代码
function Person(name) {
    this.name = name;
}

Person.prototype.sayHello = function() {
    console.log("Hello, I'm " + this.name);
};

function Student(name, school) {
    Person.call(this, name);
    this.school = school;
}

// 错误示范:Student.prototype 和 Person.prototype 是同一个对象
Student.prototype = Person.prototype;

Student.prototype.sayHello = function() {
    console.log("Hi, I'm " + this.name + " from " + this.school);
};

const person = new Person("Alice");
person.sayHello(); // 现在会输出 "Hi, I'm Alice from undefined",而不是预期的 "Hello, I'm Alice"

如上所示,当你修改 Student.prototype.sayHello 时,Person.prototype.sayHello 也会被覆盖,因为它们指向同一个对象。这种行为通常是不希望发生的。

2. 保持独立的原型链

使用 Object.create 方法时,你会创建一个新对象,这个对象的原型指向 Person.prototype,而不是直接使用 Person.prototype。这样,Student.prototype 就是一个独立的对象了,它继承了 Person.prototype 的所有属性和方法,但对它的修改不会影响 Person.prototype

javascript 复制代码
function Person(name) {
    this.name = name;
}

Person.prototype.sayHello = function() {
    console.log("Hello, I'm " + this.name);
};

function Student(name, school) {
    Person.call(this, name);
    this.school = school;
}

// 推荐方法:使用 Object.create 创建独立的原型对象
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

Student.prototype.sayHello = function() {
    console.log("Hi, I'm " + this.name + " from " + this.school);
};

const person = new Person("Alice");
person.sayHello(); // 输出: "Hello, I'm Alice"

const student = new Student("Bob", "XYZ University");
student.sayHello(); // 输出: "Hi, I'm Bob from XYZ University"

在这个例子中,Student.prototypePerson.prototype 的一个独立副本。Student.prototype 继承了 Person.prototype 的方法和属性,但对 Student.prototype 的任何修改不会影响 Person.prototype

3. 设置 constructor 属性

在使用 Object.create 时,Student.prototypeconstructor 属性会指向 Person,因为 Object.create(Person.prototype) 创建的对象继承了 Person.prototype。因此,你通常需要手动设置 Student.prototype.constructorStudent,以确保它指向正确的构造函数。

javascript 复制代码
Student.prototype.constructor = Student;

这一步虽然不是强制性的,但它可以确保 Student 的实例的 constructor 属性正确指向 Student 而不是 Person,这对于调试和代码的可读性很重要。

4. 总结

  • Object.create 的优势 :它创建了一个新对象,这个对象的原型指向 Person.prototype,使得 Student.prototypePerson.prototype 的独立副本。这样修改 Student.prototype 不会影响 Person.prototype
  • 避免副作用 :直接使用 Student.prototype = Person.prototype 会让两个构造函数的原型对象共享一个对象,导致修改其中一个时,另一个也会受到影响。
  • constructor 的设置 :使用 Object.create 之后,你需要手动将 constructor 指向 Student,以保持正确的构造函数引用。

使用 Object.create 是推荐的做法,因为它提供了更好的代码隔离和继承结构。

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
三十而立洋2 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
卡布鲁2 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
李少兄2 天前
JavaScript 隐式全局变量解析
javascript
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int2 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
沙蒿同学2 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端