Why is it necessary to set the prototype constructor?

前端 未结 14 1993
孤城傲影
孤城傲影 2020-11-22 02:54

In the section about inheritance in the MDN article Introduction to Object Oriented Javascript, I noticed they set the prototype.constructor:

// correct the          


        
14条回答
  •  眼角桃花
    2020-11-22 03:10

    So far confusion is still there.

    Following the original example, as you have an existing object student1 as:

    var student1 = new Student("Janet", "Applied Physics");
    

    Suppose you don't want to know how student1 is created, you just want another object like it, you can use the constructor property of student1 like:

    var student2 = new student1.constructor("Mark", "Object-Oriented JavaScript");
    

    Here it will fail to get the properties from Student if the constructor property is not set. Rather it will create a Person object.

提交回复
热议问题