About this and prototype

对于js中this创建的对象,相当于改变了自身的字面量,而本身有一个自带的属性为prototype,这个不属于字面量的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function Person () {
this.name = "wang";
this.age = 15;
}
Person.prototype.name = 'lala';

var john = new Person();

console.log(JSON.stringify(john));
//{name:"wang", age:15}
//这里说明了this等同于的是对象字面量


console.log(john.name)
//“wang”,这里说明了,js会优先寻找字面量里面是否有这个对象,其次才会去prototype里面寻找

对象的样子如下

1
2
3
4
5
6
7
8
{
name:'wang',
age: 15,
prototype:{
constructor:[Function],
name: 'lala'
}
}