js入门之路-----原型链

1、先看了下面的代码,差不多就能明白一二了

function Student(name) {
    this.name=name;
}

Student.prototype.say=function(){
    return 'Hello,'+this.name+'!';
}

var xiaoming=new Student("小明");
console.log(xiaoming.__proto__);
// xiaoming的原型指向Student的原型(即Student.prototype),即xiaoming.__proto__===Student.prototype
console.log(Student.prototype);
console.log(Student.prototype.__proto__);
// Student的原型(即Student.prototype)的原型指向Object的原型(即Object.prototype),即Student.prototype.__proto__===Object.prototype
console.log(Object.prototype);
console.log(Object.prototype.__proto__);
//  Object的原型(即Object.prototype)的原型指向null,即Object.prototype.__proto__===null

2、上述代码小明的原型链指向为:

xiaoming ----> Student.prototype ----> Object.prototype ----> null

xiaoming的原型指向Student的原型(即Student.prototype),Student的原型的原型指向Object的原型,Object的原型的原型指向null。如果理解不清楚,看下图就明白了()

l.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容