Discuss / JavaScript / 困惑的点找到了

困惑的点找到了

Topic source

Up.

#1 Created at ... [Delete] [Delete and Lock User]
function Student(name){
    this.name = name;
    this.hello = function(){
        console.log('hello,'+this.name);
    };
}
function PrimaryStudent(props){
    Student.call(this,props);
    this.grade = props.grade || 1;
}
PrimaryStudent.prototype = new Student();
PrimaryStudent.prototype.consturctor = PrimaryStudent;
console.log(PrimaryStudent.prototype);

廖老师的代码一直觉得怪怪的,和上面这段代码比起来空函数F有点多余,这样也能同样保证原型链呀?有没有人能解答下

Up.

#2 Created at ... [Delete] [Delete and Lock User]

[https://www.bilibili.com/video/BV1J54y1y7u9/?spm_id_from=333.337.search-card.all.click&vd_source=ad4a82ae98755f9c9297acc0d10e7d95](https://www.bilibili.com/video/BV1J54y1y7u9/?spm_id_from=333.337.search-card.all.click&vd_source=ad4a82ae98755f9c9297acc0d10e7d95)

这篇归纳总结的很好。。。。算大概搞懂为啥了链接文字

Up.

#3 Created at ... [Delete] [Delete and Lock User]

我自己的方法,子类实例化的时候,子类的prototype属性会多一份父类的实例属性和方法,父类当中的prototype不会有两份,这个是共享的

非常建议大家自己敲一遍在浏览器看看实例化后的对比,可以看下以下的区别

  1. PrimaryStudent {name: '孙墨泽', grade: 1, hello: ƒ}

  2. grade: 1

  3. hello: ƒ ()

  4. name: "孙墨泽"

  5. [[Prototype]]: Student

  6. constructor: ƒ PrimaryStudent(props)

  7. hello: ƒ ()

  8. name: undefined

  9. [[Prototype]]: Object

  10. PrimaryStudent {name: '孙墨泽', grade: 1, hello: ƒ}

  11. grade: 1

  12. hello: ƒ ()

  13. name: "孙墨泽"

  14. [[Prototype]]: Student

  15. consturctor: ƒ PrimaryStudent(props)

  16. [[Prototype]]: Object

廖雪峰

#4 Created at ... [Delete] [Delete and Lock User]

别纠结了,下一节看完,用class写,不用头大。


  • 1

Reply