117.info
人生若只如初见

JavaScript原型链有哪些方式

JavaScript 原型链主要有以下几种方式来查找和继承属性和方法:

  1. 原型链继承(Prototype Chain Inheritance)

通过将子类的原型对象(prototype)设置为父类的一个实例,从而实现继承。这种方式的优点是简单易懂,但缺点是所有子类实例共享父类的引用属性,无法实现多继承。

function Parent() {
  this.name = 'parent';
}

Parent.prototype.getName = function() {
  return this.name;
};

function Child() {
  this.age = 18;
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child1 = new Child();
console.log(child1.getName()); // 'parent'
  1. 构造函数继承(Constructor Stealing)

在子类构造函数中调用父类构造函数,并将子类实例作为上下文(this)。这种方式的优点是可以避免子类实例共享父类引用属性的问题,但缺点是无法继承父类原型上的方法。

function Parent() {
  this.name = 'parent';
}

Parent.prototype.getName = function() {
  return this.name;
};

function Child() {
  Parent.call(this);
  this.age = 18;
}

var child1 = new Child();
console.log(child1.name); // 'parent'
console.log(child1.getName); // undefined
  1. 组合继承(Combination Inheritance)

结合原型链继承和构造函数继承的优点,实现属性和方法的继承。缺点是父类构造函数会被调用两次。

function Parent(name) {
  this.name = name;
}

Parent.prototype.getName = function() {
  return this.name;
};

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child1 = new Child('parent', 18);
console.log(child1.name); // 'parent'
console.log(child1.getName()); // 'parent'
  1. 原型式继承(Prototypal Inheritance)

通过创建一个新对象并将其原型设置为父类实例,实现继承。这种方式的缺点是同样会导致子类实例共享父类引用属性。

function Parent() {
  this.name = 'parent';
}

Parent.prototype.getName = function() {
  return this.name;
};

function createObject(proto) {
  function F() {}
  F.prototype = proto;
  return new F();
}

var child1 = createObject(Parent.prototype);
child1.name = 'child';
console.log(child1.getName()); // 'child'
  1. 寄生式继承(Parasitic Inheritance)

在原型式继承的基础上,增加一个函数用于封装继承过程并添加新的方法。这种方式的缺点仍然是子类实例共享父类引用属性。

function Parent() {
  this.name = 'parent';
}

Parent.prototype.getName = function() {
  return this.name;
};

function createChild() {
  var clone = Object.create(Parent.prototype);
  clone.age = 18;
  return clone;
}

var child1 = createChild();
child1.name = 'child';
console.log(child1.getName()); // 'child'
  1. 寄生组合式继承(Parasitic Combination Inheritance)

结合寄生式继承和组合继承的优点,避免了父类构造函数被调用两次的问题。

function Parent(name) {
  this.name = name;
}

Parent.prototype.getName = function() {
  return this.name;
};

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var child1 = new Child('parent', 18);
console.log(child1.name); // 'parent'
console.log(child1.getName()); // 'parent'

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe54dAzsLBQJUBw.html

推荐文章

  • 如何启用javascript功能

    要启用JavaScript功能,您需要在浏览器中进行以下操作: 打开您的浏览器。 在浏览器的地址栏中输入"about:config"(不包括引号),然后按下Enter键。这将打开浏览...

  • javascript程序有哪些优缺点

    JavaScript的优点: 简单易学:JavaScript语法与C语言和Java类似,易于学习和理解。 跨平台:JavaScript可以在各种操作系统和浏览器上运行。 客户端脚本语言:Ja...

  • 怎么启用javascript功能

    要启用JavaScript功能,您可以按照以下步骤进行操作: 打开您的浏览器(如Chrome、Firefox、Safari等)。 在浏览器的地址栏中输入"about:config"(对于Firefox)...

  • 基本的javascript高级语法有哪些

    一些基本的JavaScript高级语法包括: 闭包:闭包是指在一个函数内部创建另一个函数,并且内部函数可以访问外部函数的变量和参数。这种特性可以用来创建私有变量和...

  • Ruby元编程如何平衡灵活性与安全

    Ruby 是一种动态、面向对象的编程语言,它支持元编程,即让代码在运行时能够修改和生成其他代码。这种灵活性使得 Ruby 成为一个非常强大的工具,但也可能导致安全...

  • Ruby元编程有哪些常见陷阱

    Ruby 元编程是一种强大的编程技巧,它允许程序在运行时动态地创建、修改和调用代码。然而,元编程也有一些常见的陷阱,如果不加以注意,可能会导致程序出现问题。...

  • Ruby元编程是否影响系统稳定性

    Ruby元编程本身不会直接影响系统的稳定性。然而,如果不正确地使用元编程技术,可能会导致一些问题,从而影响系统的稳定性。
    Ruby元编程是一种强大的编程技...

  • Ruby元编程怎样应对变化需求

    Ruby 元编程是一种强大的编程技巧,它允许程序在运行时动态地创建、修改和调用代码。这种灵活性使得 Ruby 成为了一种非常适合应对变化需求的编程语言。以下是一些...