JavaScript中__proto__与prototype的关系

Snandy

Stop, thinking is the essence of progress. html

JavaScript中__proto__与prototype的关系

这里讨论下对象的内部原型(__proto__)和构造器的原型(prototype)的关系。 git

 

1、全部构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)

1
2
3
4
5
6
7
8
9
Number.__proto__ === Function.prototype   // true
Boolean.__proto__ === Function.prototype  // true
String.__proto__ === Function.prototype   // true
Object.__proto__ === Function.prototype   // true
Function.__proto__ === Function.prototype  // true
Array.__proto__ === Function.prototype    // true
RegExp.__proto__ === Function.prototype   // true
Error.__proto__ === Function.prototype    // true
Date.__proto__ === Function.prototype     // true

 

JavaScript中有内置(build-in)构造器/对象共计12个(ES5中新加了JSON),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎建立,Math,JSON是以对象形式存在的,无需new。它们的__proto__是Object.prototype。以下 github

1
2
Math.__proto__ === Object.prototype   // true
JSON.__proto__ === Object.prototype   // true

 

上面说的“全部构造器/函数”固然包括自定义的。以下 chrome

1
2
3
4
5
6
// 函数声明
function  Person() {}
// 函数表达式
var  Man =  function() {}
console.log(Person.__proto__ === Function.prototype)  // true
console.log(Man.__proto__ === Function.prototype)     // true

 

这说明什么呢? 编程

全部的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身。全部构造器都继承了Function.prototype的属性及方法。如length、call、apply、bind(ES5)。 浏览器

 

Function.prototype也是惟一一个typeof XXX.prototype为 “function”的prototype。其它的构造器的prototype都是一个对象。以下 app

1
2
3
4
5
6
7
8
9
10
console.log(typeof  Function.prototype)  // function
console.log(typeof  Object.prototype)    // object
console.log(typeof  Number.prototype)    // object
console.log(typeof  Boolean.prototype)   // object
console.log(typeof  String.prototype)    // object
console.log(typeof  Array.prototype)     // object
console.log(typeof  RegExp.prototype)    // object
console.log(typeof  Error.prototype)     // object
console.log(typeof  Date.prototype)      // object
console.log(typeof  Object.prototype)    // object

  

噢,上面还提到它是一个空的函数,alert(Function.prototype) 下看看。 函数

 

知道了全部构造器(含内置及自定义)的__proto__都是Function.prototype,那Function.prototype的__proto__是谁呢? 测试

 

相信都据说过JavaScript中函数也是一等公民,那从哪能体现呢?以下 ui

1
console.log(Function.prototype.__proto__ === Object.prototype)  // true

这说明全部的构造器也都是一个普通JS对象,能够给构造器添加/删除属性等。同时它也继承了Object.prototype上的全部方法:toString、valueOf、hasOwnProperty等。

 

最后Object.prototype的__proto__是谁?

1
Object.prototype.__proto__ ===  null   // true

已经到顶了,为null。

 

2、全部对象的__proto__都指向其构造器的prototype

上面测试了全部内置构造器及自定义构造器的__proto__,下面再看看全部这些构造器的实例对象的__proto__指向谁?

 

先看看JavaScript引擎内置构造器

1
2
3
4
5
6
7
8
9
10
11
var  obj = {name:  'jack'}
var  arr = [1,2,3]
var  reg = /hello/g
var  date =  new  Date
var  err =  new  Error('exception')
 
console.log(obj.__proto__ === Object.prototype)  // true
console.log(arr.__proto__ === Array.prototype)   // true
console.log(reg.__proto__ === RegExp.prototype)  // true
console.log(date.__proto__ === Date.prototype)   // true
console.log(err.__proto__ === Error.prototype)   // true

 

再看看自定义的构造器,这里定义了一个Person

1
2
3
4
5
function  Person(name) {
    this.name = name
}
var  p =  new  Person('jack')
console.log(p.__proto__ === Person.prototype)  // true

p是Person的实例对象,p的内部原型老是指向其构造器Person的prototype。

 

每一个对象(无论是Object对象仍是Function对象)都有一个constructor属性,能够获取它的构造器,所以如下打印结果也是恒等的

1
2
3
4
5
function  Person(name) {
    this.name = name
}
var  p =  new  Person('jack')
console.log(p.__proto__ === p.constructor.prototype)  // true

 

关于什么是原型:原型就是prototype属性:
标准对象原型访问器Object.getPrototype(object),到目前为止只有Firefox和chrome实现了此访问器。除了IE,其余的浏览器支持非标准的访问器__proto__,而prototype则是一个只有函数才具备的属性

上面的Person没有给其原型添加属性或方法,这里给其原型添加一个getName方法

1
2
3
4
5
6
7
8
function  Person(name) {
    this.name = name
}
// 修改原型
Person.prototype.getName =  function() {}
var  p =  new  Person('jack')
console.log(p.__proto__ === Person.prototype)  // true
console.log(p.__proto__ === p.constructor.prototype)  // true

能够看到p.__proto__与Person.prototype,p.constructor.prototype都是恒等的,即都指向同一个对象。

 

若是换一种方式设置原型,结果就有些不一样了

1
2
3
4
5
6
7
8
9
10
function  Person(name) {
    this.name = name
}
// 重写原型
Person.prototype = {
    getName:  function() {}
}
var  p =  new  Person('jack')
console.log(p.__proto__ === Person.prototype)  // true
console.log(p.__proto__ === p.constructor.prototype)  // false

这里直接重写了Person.prototype(注意:上一个示例是修改原型)。输出结果能够看出p.__proto__仍然指向的是Person.prototype,而不是p.constructor.prototype。

这也很好理解,给Person.prototype赋值的是一个对象直接量{getName: function(){}},使用对象直接量方式定义的对象其构造器(constructor)指向的是根构造器Object,Object.prototype是一个空对象{},{}天然与{getName: function(){}}不等。以下

1
2
3
4
var  p = {}
console.log(Object.prototype)  // 为一个空的对象{}
console.log(p.constructor === Object)  // 对象直接量方式定义的对象其constructor为Object
console.log(p.constructor.prototype === Object.prototype)  // 为true,不解释 

 

上面代码中用到的__proto__目前在IE6/7/8/9中都不支持。IE9中能够使用Object.getPrototypeOf(ES5)获取对象的内部原型。

1
2
3
var  p = {}
var  __proto__ = Object.getPrototypeOf(p)
console.log(__proto__ === Object.prototype)  // true

  

===【补充】==================================

prototype 是原型,是Function对象才有的属性,原型主要是用来实现继承,从而用js 进行面向对象编程
如 Foo 是一个Function对象,是有prototype属性的 ,它指向一个Boo 的实例对象
而p 是一个 Foo 的实例对象,不是一个Function 对象,没有prototype 属性,可是p有一个私有属性__proto__ 这个属性,通常不直接访问的,事实上这个属性就是 Foo.prototype
在chrome控制台中测试
console.log(p.__proto__ == Foo.prototype)
VM952:2 true
console.log(p.__proto__ === Foo.prototype)
VM956:2 true
上述测试都输出true,说明他们确实是同一个东西

js的继承是经过原型继承的,有个原型链的概念,而原型链就是经过__proto__属性实现的
function Boo() {this.x="test in Boo";}
function Foo() {this.y = "test in Foo"}
Foo.prototype = new Boo;
p=new Foo;
console.log(p.y);//test in Foo

console.log(p.x);//test in Boo
Foo 中并无属性x,当p找不到属性x时,就到p.__proto__属性(指向原型)中找,原型中是有x属性的,因而输出x的值
若是不考虑内部实现,就好像属性x是从Boo中继承的同样
console.log(p.__proto__);   输出 Boo{}表示一个空的Boo对象(经过 new Boo 建立的 )

p.__proto__.p.__proto__这个会报错,由于p.__proto__中没有名为p的属性

function Foo() {}
p=new Foo;
console.log(p.__proto__);
由于没有改变Foo.prototype 的属性,因此仍然指向它本身(Foo)
而 p.__proto__ 就是 Foo.prototype 全部输出Foo{} 表示一个空的Foo对象
相关文章
相关标签/搜索