1)原型
2)原型链:查找对象的属性和方法
1、对象本身找
2、构造函数找
3、对象原型找
4、构造函数原型找
5、向上原型中查找------》一直向上找,直到找不到 | 找到为止
原型链顶端是null,对象找不到属性是undefined
function Fun(){
this.test1=function(){
console.log('test1');
}
}
Fun.prototype.test2=function(){
console.log('test2');
}
var fun=new Fun()
fun.test1()//自己的属性
fun.test2()//原型链上找到的
console.log(fun.toString());//构造函数Fun的原型对象,也是object的一个实例化对象,
//顺着这条原型链能找到Object上的toString方法
3)JS中对原型的修改与重写
修改
function Person(name){
this.name=name
}
Person.prototype.getName=function(){
console.log(ths.name);
}
var p=new Person('张三')
console.log(p.__proto__===Person.prototype);//true
console.log(p.constructor);//ƒ Person(name){this.name = name}
console.log(p.__proto__===p.constructor.prototype);//true
重写
直接给Person的原型对象用对象进行赋值,p的构造函数指向根构造函数object
function Person(name){
this.name=name
}
//重写原型
Person.prototype={
getName:function(){
console.log(this.name);
}
}
var p=new Person('张三')
console.log(p.__proto__===Person.prototype);//true
console.log(p.__proto__);//{getName:f}
console.log(p.constructor.prototype);//{costructor:...}
console.log(p.__proto__===p.constructor.prototype);//false
p.constructor=Person
console.log(p.__proto__ === p.constructor.prototype);//true
题: