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。如果理解不清楚,看下图就明白了()

js入门之路-----原型链_第1张图片
l.png

你可能感兴趣的:(js入门之路-----原型链)