原型 prototype

🌈 prototype 是函数的属性,其值是个对象

function Handphone() {}
console.log(Handphone.prototype);

Untitled

prototype 是构造函数中的,构造出的每个实例对象的公共祖先

所有被该构造函数构造出来的对象,都可以继承原型上的属性和方法

function Handphone(color, brand) {
	this.color = color;
  this.brand = brand;
}
Handphone.prototype.rom = '64G';
Handphone.prototype.ram = '6G';

var hp1 = new Handphone('red', '小米');
var hp2 = new Handphone('black', 'huawei');

console.log(hp1.rom);
console.log(hp2.ram);

Untitled

🌈 constructor 指向构造函数本身

function Handphone(color, brand) {
	this.color = color;
  this.brand = brand;
}
console.dir(Handphone);
console.dir(Handphone.prototype.constructor);

Untitled

constructor 可以被修改

function Telephone() {}
function Handphone(color, brand) {
	this.color = color;
  this.brand = brand;
}
Handphone.prototype = {
	constructor: Telephone
}
console.log(Handphone.prototype)

Untitled

🌈 proto 是实例化以后的结果

function Car() {
	var this = {
  	__proto__: Car.prototype
  }
}
Car.prototype.name = 'Benz';
var car = new Car();
console.log(car);

Untitled

function Person() {}
Person.prototype.name = '张三';

var p1 = {
	name: 'lance'
}

var person = new Person();
console.log(person.name);
person.__proto__ = p1;
console.log(person.name);

Untitled

🌈🌈🌈 实例化对象以后再来修改构造函数的 prototype,不影响该对象的原型属性 ,因为修改后的 prototype 指向了新的对象,不影响原来的 prototype,但影响原来 prototype 下的 constructor