JS原型与原型链
普通对象与函数对象
var o1 = {};
var o2 =new Object();
var o3 = new f1();
function f1(){};
var f2 = function(){};
var f3 = new Function('str','console.log(str)');
console.log(typeof Object); //function
console.log(typeof Function); //function
console.log(typeof f1); //function
console.log(typeof f2); //function
console.log(typeof f3); //function
console.log(typeof o1); //object
console.log(typeof o2); //object
console.log(typeof o3); //object
总结:
通过new Function 创建的都是函数对象,其他都是普通对象
构造函数
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.sayName = function() { alert(this.name) }
}
var person1 = new Person('Zaxlct', 28, 'Software Engineer');
var person2 = new Person('Mick', 23, 'Doctor');
上面例子,person1和person2都是Person的实例。这两个实例都有一个constructor(构造函数)属性,该属性(是一个指针)指向Person
console.log(person1.constructor == Person); //true
console.log(person2.constructor == Person); //true
总结:
person1和person2都是构造函数Person的实例
实例的构造函数属性(constructor)指向构造函数
原型对象
每当定义一个对象(函数也是对象)的时候,对象中会包含一些预定义的属性。其中每个函数对象都有一个prototype属性,这个属性指向函数的原型对象
定律:
每个对象都有_proto_属性,但只有函数对象有prototype属性
什么是原型对象?吧上面的例子改一下
Person.prototype = {
name: 'Zaxlct',
age: 28,
job: 'Software Engineer',
sayName: function() {
alert(this.name);
}
}
Person.prototype 就是原型对象,除了上面的属性,他还有一个默认的隐性属性:constructor
在默认情况下,所有的原型对象都会自动获得一个 constructor(构造函数)属性,这个属性(是一个指针)指向 prototype 属性所在的函数(Person)
定律:
Person.prototype.constructor == Person
person1.constructor == Person
Person.prototype.constructor == Person
person1 为什么有 constructor 属性?那是因为 person1 是 Person 的实例。 那 Person.prototype 为什么有 constructor 属性??同理, Person.prototype (你把它想象成 A) 也是Person 的实例。
所以,其实是Person在创建的时候,内部自己创建了一个她的实例对象并赋值给他的prototype
var A = new Person();
Person.prototype = A;
所以:原型对象(Person.prototype)是 构造函数(Person)的一个实例
原型对象其实就是普通对象(但 Function.prototype 除外,它是函数对象,但它很特殊,他没有prototype属性(前面说道函数对象都有prototype属性))
function Person(){};
console.log(Person.prototype) //Person{}
console.log(typeof Person.prototype) //Object
console.log(typeof Function.prototype) // Function,这个特殊
console.log(typeof Object.prototype) // Object
console.log(typeof Function.prototype.prototype) //undefined
问:原型对象是用来做什么的?
主要作用是用于继承
var Person = function(name){
this.name = name; // tip: 当函数执行时这个 this 指的是谁?
};
Person.prototype.getName = function(){
return this.name; // tip: 当函数执行时这个 this 指的是谁?
}
var person1 = new person('Mick');
person1.getName(); //Mick
从这个例子可以看出,通过给 Person.prototype 设置了一个函数对象的属性,那有 Person 的实例(person1)出来的普通对象就继承了这个属性
proto
JS在创建对象的时候,都有一个叫做_proto_的内置属性,用于指向创建他的构造函数的原型对象。
对象 person1 有一个 __proto__属性,创建它的构造函数是 Person,构造函数的原型对象是 Person.prototype ,所以:
person1.__proto__ == Person.prototype
如图:
总结:
Person.prototype.constructor == Person;
person1.__proto__ == Person.prototype;
person1.constructor == Person;
不过,要明确的真正重要的一点就是,这个连接存在于实例(person1)与构造函数(Person)的原型对象(Person.prototype)之间,而不是存在于实例(person1)与构造函数(Person)之间。
注:
Object.prototype 对象也有proto属性,但它比较特殊,为 null 。因为 null 处于原型链的顶端,这个只能记住。
Object.prototype.__proto__ === null
函数对象
所有函数对象的proto都指向Function.prototype,他是一个空函数(Empty function)
Number.__proto__ === Function.prototype // true
Number.constructor == Function //true
Boolean.__proto__ === Function.prototype // true
Boolean.constructor == Function //true
String.__proto__ === Function.prototype // true
String.constructor == Function //true
// 所有的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身
Object.__proto__ === Function.prototype // true
Object.constructor == Function // true
// 所有的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身
Function.__proto__ === Function.prototype // true
Function.constructor == Function //true
Array.__proto__ === Function.prototype // true
Array.constructor == Function //true
RegExp.__proto__ === Function.prototype // true
RegExp.constructor == Function //true
Error.__proto__ === Function.prototype // true
Error.constructor == Function //true
Date.__proto__ === Function.prototype // true
Date.constructor == Function //true
整理来说:
我们定义一个对象obj={}
取对象obj.a 的属性
根据原型链,我们先取obj的显性属性,找不到。我们取obj.proto 里的a属性,也就是obj上一级的prototype里找a, 如果找不到,我们再去上一级的prototype的_proto_找a,也就是上上级的prototype,直到找到或者到Object.prototype.proto == null,结束。
所以:原型链是通过_proto_传递
参考: