function Student(age,name){
this.age = age;
this.name = name;
}
var that;
Student.prototype.score = function(){
console.log(‘孩子们成绩都很好!’);
that = this;
}
var xl = new Student(18,‘小熊’);
xl.score();
console.log(that === xl);
定义一个全局变量that,在score
函数里面给它赋值,让其指向函数里面的this,调用实例对象的score
方法,判断that和实例对象是否一致,如果一致,则说明原型对象this的指向是该实例。
打印结果为:
即原型对象里面放的是方法, 这个方法里面的this 指向的是 这个方法的调用者, 也就是这个实例对象。
====================