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

时间:2015-09-04 19:43:47

标签: javascript constructor prototype

如果我有JavaScript功能:

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

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

  3. 如果我有代码:

    function Student(){}
    Student.prototype = new Person()
    Student.prototype.constructor = Student
    
    1. 为什么要继承Student from Person,我必须将Student.prototype.constructor设置为Student

3 个答案:

答案 0 :(得分:0)

  
      
  1. 在此之后Person.prototype的值是什么?
  2.   

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

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

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

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

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

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

您仍然在调用构建函数,而function正在使用new

答案 1 :(得分:0)

您的问题的答案将以微观细节进行解释,因此请查看本文,它是最好的文章之一Prototype。我使用了该文章中的所有答案

  

在此之后Person.prototype的值是什么?

 function Person(){}

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

此原型属性是 对象 ,它具有名为 构造函数 的非可枚举属性 < em>可写 可配置

这个构造函数只是对Function本身的引用,在这种情况下 Person 因此,如果您选中Person.prototype.constructor == PersonPerson.constructor == Person。它会回归真实。 (后者与第一个表达式相同,通过属性委托)这将回答您的第二个问题。

  

Person.constructor和Person.prototype.constructor有什么区别?

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

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

它将首先检查Person.prototype,因为没有名为x的属性。它会检查父母。哪个 Object.prototype (所有对象都是原型的。这将是你祖先链中的最后一个祖先)

  

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

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

Student.prototype.constructor = Student

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

的实例

var bob = new Student()

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

如果你的想法仍然不清楚这个答案。请参考以上链接。关于原型

答案 2 :(得分:0)

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

    • 这两者完全不同。首先,
      _
    • 当使用Person创建实例时,Person.prototype.constructor再次发挥作用。使用Person创建的所有实例将共享相同的构造函数,这只是Person函数本身
    • 如果我们继续
      constructor is not Person's property, it is inherited from Person.__proto__.
    • ,我们会遇到循环引用
  3. 为什么要设置Student.prototype.constructor =学生?

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

      • _
      • 这是因为,当创建实例时,实例不会拥有构造函数属性,它只是继承自原型,在这种情况下,原型是Person对象,因此,
        Person.prototype.constructor.prototype..... This is because, Person.protoype.constructor refers to Person function itself
    • 案例2:设置构造函数

      • 当Student.prototype.constructor设置为Student时,创建的所有实例都会在检查s.prototype.constructor或s.constructor时正确显示函数Student(){}。
相关问题