亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

isPrototypeOf的問題

isPrototypeOf的問題

皈依舞 2018-09-17 06:33:22
const print = require('./print.js');class Student {    constructor(name) {        this.name = name;     }     hello() {         alert('Hello, ' + this.name + '!');     } }class PrimaryStudent extends Student {    constructor(name, grade) {        super(name); // 記得用super調用父類的構造方法!         this.grade = grade;     }     myGrade() {         print('I am at grade ' + this.grade);     } }let a = new PrimaryStudent({    name: 'tst',    grade: 100}) print(Student.prototype.isPrototypeOf(a)); print(Student.isPrototypeOf(a)); print(a instanceof Student);res:truefalsetrue對比原型式繼承的方式var o1 = {}; var o2 = Object.create(o1); var o3 = Object.create(o2);o2.isPrototypeOf(o3) // trueo1.isPrototypeOf(o3) // true為什么會有不同的結果
查看完整描述

1 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

這兩個繼承方式內在都是通過原型繼承,結果也是一致的。

print(Student.prototype.isPrototypeOf(a));print(PrimaryStudent.prototype.isPrototypeOf(a));

都是true

print(Student.isPrototypeOf(a));
Student是構造函數,他不是a的原型,Student.prototype才是,所以自然是返回false。
我覺得你給的例子很清晰,應該沒啥奇怪的才對。


查看完整回答
反對 回復 2018-10-17
  • 1 回答
  • 0 關注
  • 483 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號