prototype

构造函数、实例对象、原型对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
构造函数、实例对象、原型对象
构造函数没有实例化之前就是普通函数 this指向window
实例化过程中 new 把this指向先变的无指向(破坏this) 然后再复制一份同样的
数据进行赋值给实例化之后的变量
实例对象是通过构造函数实例来的
只要是实例化之后 就一定有原型对象
prototype: 通过当前的原型进行数据的传递 共享数据 实现继承 节省内存空间
function Parent(name,age,color){
this.name = name
this.age = age
this.color = colorm
}
var p = new Parent(1,2,3)
console.log(p.constructor === p.__proto__.constructor)
console.log(p.__proto__ === Parent.prototype)
console.log(p.__proto__.constructor === Parent)
console.log(Parent.prototype.constructor === Parent)
console.log(p.__proto__.constructor === Parent.prototype.constructor)
console.log(p.__proto__.__proto__)
console.log(Parent.prototype.__proto__ === Object.prototype)
console.log(Object.prototype)
console.log(p.__proto__.__proto__ === Object.prototype)