原型和原型链
一. 普通对象与函数对象JavaScript 中,万物皆对象!但对象也是有区别的。分为普通对象和函数对象,Object 、Function 是 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
在上面的例子中 o1 o2 o3 为普通对象,f1 f2 f3 为函数对象。怎么区分,其实很简单,凡是通过 new Function() 创建的对象都是函数对象,其他的都是普通对象。f1,f2,归根结底都是通过 new Function()的方式进行创建的。Function 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 的实例。这两个实例都有一个 console.log(person1.constructor == Person); //true console.log(person2.constructor == Person); //true
我们要记住两个概念(构造函数,实例): 三. 原型对象在 JavaScript 中,每当定义一个对象(函数也是对象)时候,对象中都会包含一些预定义的属性。其中每个函数对象都有一个 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: 在上面我们给 A 添加了 四个属性:name、age、job、sayName。其实它还有一个默认的属性:
上面这句话有点拗口,我们「翻译」一下:A 有一个默认的 在上面第二小节《构造函数》里,我们知道实例的构造函数属性(constructor)指向构造函数 : 这两个「公式」好像有点联系: person1.constructor == Person Person.prototype.constructor == Person person1 为什么有 constructor 属性?那是因为 person1 是 Person 的实例。 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 A = new Function (); Function.prototype = A;
上文提到凡是通过 new Function( ) 产生的对象都是函数对象。因为 A 是函数对象,所以
|