原型链1
JavaScript对每个创建的对象都会设置一个原型,指向它的原型对象。
1.当我们访问这个对象的属性时,首先在当前对象上查找改属性,当没有找到时,会去该对象的原型对象上找,比如:
function stu(name){
this.name = name;
this.show = function(){
console.log("我叫"+this.name);
}
}
var s1 = new stu("zyt");
s1.show(); //我叫zyt
s1.name="nnn";
s1.show();//我叫nnn
2.原型指向
function stu(name){
this.name = name;
this.show = function(){
console.log("我叫"+this.name);
}
}
var s1 = new stu("zyt");
s1.show();
s1.name="nnn";
s1.show();
console.log(s1.__proto__); //f 对象的__proto__指向构造函数的prototype,即stu.prototype
console.log(s1.__proto__ === stu.prototype); //true
//以此类推,构造函数的__proto__指向Function的prototype,即Function.prototype
console.log(stu.__proto__ === Function.prototype); //true
//最后 Array和Function的原型对象的__proto__都指向Object.prototype
console.log(Array.prototype.__proto__ === Object.prototype); //true
console.log(Function.prototype.__proto__ === Object.prototype); //true

浙公网安备 33010602011771号