在javascript中,(CONSTRUCTOR).prototype.constructor =(ANOTHER CONSTRUCTOR)被异步调用了吗?

时间:2019-06-30 13:23:53

标签: javascript constructor prototype

我正在学习javascript原型继承,并且遇到了奇怪的行为。有人可以帮忙消除下面的困惑吗?

似乎该语句: Magazine.prototype.constructor = Magazine; 正在创建对象mag1之前执行

//Book constructor.
function Book (title, author, year) {
    this.title = title;
    this.author = author;
    this.year = year;
}

//Magazine constructor.
function Magazine(title, author, year, month) {
    //We create the same properties/functions as book in here using the call method (This is not  inheritance here)
    Book.call(this, title, author, year);
    this.month = month;
}

Magazine.prototype = Book.prototype;

const mag1 = new Magazine('Magazine 1', 'John Doe', '2017', 'January');
console.log(mag1);  //mag1.__proto__  object has Book constructor here

//But if we enable the commented line below, mag1.__proto__  object above, shows Magazine constructor, even though the the line below is executed afterwards.
//Is the following line executing asynchronously?

//Magazine.prototype.constructor = Magazine;
console.log(mag1);

我希望mag1的第一个控制台输出 proto 对象应该具有Book构造函数。

mag1的第二个控制台输出, proto 对象应该具有Magazine构造函数。

0 个答案:

没有答案
相关问题