关于javascript原型和构造函数的一些问题

Some questions about javascript prototypes and constructors

本文关键字:问题 构造函数 javascript 原型 关于      更新时间:2023-09-26

如果我有JavaScript函数:

function Person(){}
  1. 在此之后,Person.prototype的值是多少?

  2. Person.constructorPerson.prototype.constructor之间有什么区别?

如果我有代码:

function Student(){}
Student.prototype = new Person()
Student.prototype.constructor = Student
  1. 为什么在从人继承学生时必须将Student.prototype.constructor设置为Student

你的问题的答案已经详细解释了,所以看看这篇文章,它是最好的文章原型之一。我已经使用了那篇文章中的所有答案

在这之后,Person.prototype的值是多少?

 function Person(){}

每当定义函数时,它都有一个名为*prototype的属性,该属性是不可枚举的。

此原型属性是一个对象,该对象具有名为构造函数的不可枚举属性,该属性是可写>可配置[/strong>。

这个构造函数只是对函数本身的引用,在这种情况下它是Person因此,如果您检查Person.prototype.constructor == PersonPerson.constructor == Person。它将返回true。(后一个与第一个表达式相同,通过财产委托(这回答了您的第二个问题。

Person.constructor和Person.prototype.constructors之间有什么区别?

Javascript属性委派,当您尝试访问对象中不存在的属性时,它将尝试检查其原型链接。

当您尝试访问Person.prototype对象上名为x的属性时。

它将首先签入Person.prototype,因为没有名为x的属性。它将签入parent。对象原型(所有对象都是原型化的。这将是祖先链中的最后一个祖先(

第三个问题是关于创建继承Person类的子类或对象

默认情况下,Student函数具有原型对象。执行此语句时,new Person()将创建一个对象,并将创建的对象的引用传递给Person.call(this),并将对象原型链接到Person.prototype。Student.prototype=新人物((此方法用于模拟子类继承。这样,从*new Student((创建的新对象也可以通过原型链接访问Person类方法和变量。

Student.prototype.constructor = Student

当你创建一个新的Student对象来检查继承时,需要这个语句,这样新对象就是Student Class 的实例

var bob = new Student()

所以当你想检查bob instanceofStudent时。在后台,这将执行bob.constructor == Student。由于bob没有构造函数属性,它将委托并签入Student。

如果你仍然不清楚这个答案。请参阅上面的链接。关于原型

  1. 在这之后,Person.prototype的值是多少

具有指向函数对象的属性constructor的对象。

  1. Person.constructor和Person.prototype.constructors之间有什么区别

一个是函数对象的一部分,而另一个是原型对象的一个,它将用于链接原型。在Person上使用new操作数时,此新对象不会继承constructor

function Student(){}
 Student.prototype = new Person()
 Student.prototype.constructor = Student

您正在创建Person的实例,并将其指定为Student的原型。通过这样做,您也继承了Person的构造函数,您可能想要使用Student的构造函数。

通过创建Person的新对象,您还可以指定构造函数。否则,如果您先执行Student.prototype = Person.prototype,然后执行Student.prototype.constructor = Student,您也会将Person.prototype.constructor更改为Student

您仍在调用构造函数,即您在上使用newfunction

function Person(){}
  1. Person.prototype的值是多少

    • 创建函数时,prototype属性将作为空对象设置为该函数
    • 这个原型属性(空对象(在创建该函数的实例时发挥作用
    • 对于新创建的实例,
       instance.__proto__ or Object.prototypeOf(instance) would return the Person.prototype 
    • 基本上,创建的实例的父级是Person.prototype
  2. Person.constructor和Person.prototype.constructor

    • 这两者完全不同。首先,
      constructor is not Person's property, it is inherited from Person.__proto__.
    • 当使用Person创建实例时,Person.prototype.constructor再次发挥作用。所有使用Person创建的实例都将共享同一个构造函数,该构造函数不过是Person函数本身
    • 如果我们继续
      Person.prototype.constructor.prototype..... This is because, Person.protoype.constructor refers to Person function itself
      ,我们将遇到循环引用
  3. 为什么设置Student.protype.constructor=Student?

    • 案例1:未设置构造函数

      •  s = new Student();
        s.constructor // returns function Person(){} 
      • 这是因为,当创建实例时,实例将不拥有构造函数属性,它只是从原型继承而来,在这种情况下,原型是Person对象,因此,
        s.constructor which is s.prototype.constructor would show function Person(){}
    • 案例2:设置构造函数

      • 当Student.prototype.constructor设置为Student时,当选中s.prototype.constructionor或s.constructor时,所有创建的实例都将正确显示函数Student(({}