js中的原型,原型链和继承

在传统的基于Class的语言如Java、C++中,继承的本质是扩展一个已有的Class,并生成新的Subclass。javascript

因为这类语言严格区分类和实例,继承其实是类型的扩展。可是,JavaScript最开始是没有类的概念的咱们没法直接扩展一个Class,由于根本不存在Class这种类型所以采用原型继承这种方法。php

首先了解一些概念:java

普通对象与函数对象

构造函数 

原型对象

一.普通对象与函数对象

Object 、Function 是 JS 自带的函数对象,凡是经过 new Function() 建立的对象都是函数对象,其余的都是普通对象。Function Object 也都是经过 New Function()建立的。函数

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
 o1 o2 o3 为普通对象,f1 f2 f3 为函数对象

二. 构造函数

咱们先复习一下构造函数的知识:ui

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。 即:this

console.log(person1.constructor == Person); //true console.log(person2.constructor == Person); //true 

咱们要记住两个概念(构造函数,实例):
person1 和 person2 都是 构造函数 Person 的实例
一个公式:
实例的构造函数属性(constructor)指向构造函数。spa


三. 原型对象

在 JavaScript 中,每当定义一个对象(函数也是对象)时候,对象中都会包含一些预约义的属性。其中每一个函数对象都有一个prototype 属性,这个属性指向函数的原型对象。(先用无论什么是 __proto__ 第二节的课程会详细的剖析)prototype

function Person() {} Person.prototype.name = 'Zaxlct'; Person.prototype.age = 28; Person.prototype.job = 'Software Engineer'; Person.prototype.sayName = function() { alert(this.name); } var person1 = new Person(); person1.sayName(); // 'Zaxlct' var person2 = new Person(); person2.sayName(); // 'Zaxlct' console.log(person1.sayName == person2.sayName); //true 

咱们获得了本文第一个「定律」:翻译

每一个对象都有 __proto__ 属性,但只有函数对象才有 prototype 属性

那什么是原型对象呢?
咱们把上面的例子改一改你就会明白了:指针

Person.prototype = {
   name: 'Zaxlct', age: 28, job: 'Software Engineer', sayName: function() { alert(this.name); } } 

原型对象,顾名思义,它就是一个普通对象(废话 = =!)。从如今开始你要紧紧记住原型对象就是 Person.prototype ,若是你仍是惧怕它,那就把它想一想成一个字母 A: var A = Person.prototype


在上面咱们给 A 添加了 四个属性:name、age、job、sayName。其实它还有一个默认的属性:constructor

在默认状况下,全部的原型对象都会自动得到一个 constructor(构造函数)属性,这个属性(是一个指针)指向 prototype 属性所在的函数(Person)

上面这句话有点拗口,咱们「翻译」一下:A 有一个默认的 constructor 属性,这个属性是一个指针,指向 Person。即:
Person.prototype.constructor == Person


在上面第二小节《构造函数》里,咱们知道实例的构造函数属性(constructor)指向构造函数person1.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 

Function.prototype 为何是函数对象呢?

var A = new Function (); Function.prototype = A; 

上文提到凡是经过 new Function( ) 产生的对象都是函数对象。由于 A 是函数对象,因此Function.prototype 是函数对象。

那原型对象是用来作什么的呢?主要做用是用于继承。举个例子:

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)出来的普通对象就继承了这个属性。具体是怎么实现的继承,就要讲到下面的原型链了。

小问题,上面两个 this 都指向谁?

var person1 = new person('Mick'); person1.name = 'Mick'; // 此时 person1 已经有 name 这个属性了 person1.getName(); //Mick 

故两次 this 在函数执行时都指向 person1。



四.继承

首先咱们须要一个构造函数:

function Student(props) { this.name = props.name || 'Unnamed'; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); }

还须要这个函数的原型链:

Student的原型链:

js-proto

基于Student扩展出PrimaryStudent,能够先定义出PrimaryStudent




function PrimaryStudent(props) { // 调用Student构造函数,绑定this变量: Student.call(this, props); this.grade = props.grade || 1; }

可是,调用了Student构造函数不等于继承了StudentPrimaryStudent建立的对象的原型是:

new PrimaryStudent() ----> PrimaryStudent.prototype ----> Object.prototype ----> null 

必须想办法把原型链修改成:

new PrimaryStudent() ----> PrimaryStudent.prototype ----> Student.prototype ----> Object.prototype ----> null 

这样,原型链对了,继承关系就对了。新的基于PrimaryStudent建立的对象不但能调用PrimaryStudent.prototype定义的方法,也能够调用Student.prototype定义的方法。

若是你想用最简单粗暴的方法这么干:

PrimaryStudent.prototype = Student.prototype;

是不行的!若是这样的话,PrimaryStudentStudent共享一个原型对象,那还要定义PrimaryStudent干啥?

咱们必须借助一个中间对象来实现正确的原型链,这个中间对象的原型要指向Student.prototype。为了实现这一点,参考道爷(就是发明JSON的那个道格拉斯)的代码,中间对象能够用一个空函数F来实现:

// PrimaryStudent构造函数: function PrimaryStudent(props) { Student.call(this, props); this.grade = props.grade || 1; } // 空函数F: function F() { } // 把F的原型指向Student.prototype: F.prototype = Student.prototype; // 把PrimaryStudent的原型指向一个新的F对象,F对象的原型正好指向Student.prototype: PrimaryStudent.prototype = new F(); // 把PrimaryStudent原型的构造函数修复为PrimaryStudent: PrimaryStudent.prototype.constructor = PrimaryStudent; // 继续在PrimaryStudent原型(就是new F()对象)上定义方法: PrimaryStudent.prototype.getGrade = function () { return this.grade; }; // 建立xiaoming: var xiaoming = new PrimaryStudent({ name: '小明', grade: 2 }); xiaoming.name; // '小明' xiaoming.grade; // 2 // 验证原型: xiaoming.__proto__ === PrimaryStudent.prototype; // true xiaoming.__proto__.__proto__ === Student.prototype; // true // 验证继承关系: xiaoming instanceof PrimaryStudent; // true xiaoming instanceof Student; // true 

用一张图来表示新的原型链:

js-proto-extend

注意,函数F仅用于桥接,咱们仅建立了一个new F()实例,并且,没有改变原有的Student定义的原型链。

若是把继承这个动做用一个inherits()函数封装起来,还能够隐藏F的定义,并简化代码:

function inherits(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; } 

这个inherits()函数能够复用:

function Student(props) { this.name = props.name || 'Unnamed'; } Student.prototype.hello = function () { alert('Hello, ' + this.name + '!'); } function PrimaryStudent(props) { Student.call(this, props); this.grade = props.grade || 1; } // 实现原型继承链: inherits(PrimaryStudent, Student); // 绑定其余方法到PrimaryStudent原型: PrimaryStudent.prototype.getGrade = function () { return this.grade; }; 

小结

JavaScript的原型继承实现方式就是:

  1. 定义新的构造函数,并在内部用call()调用但愿“继承”的构造函数,并绑定this

  2. 借助中间函数F实现原型链继承,最好经过封装的inherits函数完成;

  3. 继续在新的构造函数的原型上定义新方法。

相关文章
相关标签/搜索