一、所有构造器/函数的__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。如下

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

上面说的“所有构造器/函数”当然包括自定义的。如下

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都是一个对象。如下

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中函数也是一等公民,那从哪能体现呢?如下

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。

二、所有对象的__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

console.log(obj.constructor);//Object
console.log(arr.constructor);//Array
console.log(reg.constructor);//RegExp
console.log(date.constructor);//Date
console.log(err.constructor);//Error
console.log(obj.__proto__===Object.prototype);//指向原型 true
console.log(obj.constructor===Object);//指向构造器
console.log(arr.constructor===Array);//true
console.log(reg.constructor===RegExp);//true
console.log(date.constructor===Date);//true
console.log(err.constructor===Error);//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。

每个对象都有一个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

上面的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

JavaScript中__proto__与prototype的关系的更多相关文章

  1. JavaScript中__proto__与prototype的关系(转)

    一.所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function) 1 2 3 4 5 6 7 8 9 Number.__proto__ ...

  2. JavaScript中的Array.prototype.slice.call()方法学习

    JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外 ...

  3. js中__proto__和prototype的区别和关系?

    _proto__(隐式原型)与prototype(显式原型)1.是什么 显式原型 explicit prototype property: 每一个函数在创建之后都会拥有一个名为prototype的属性 ...

  4. js中__proto__和prototype的区别和关系? 这样好理解多了

    原型的概念 真正理解什么是原型是学习原型理论的关键.很多人在此产生了混淆,没有真正理解,自然后续疑惑更多. 首先,我们明确原型是一个对象,其次,最重要的是, Every function has a ...

  5. Javascript中函数调用和this的关系

    例子先行: var myObject={ foo:"bar", func:function(){ var self=this; console.log("outerfun ...

  6. 【你不知道的javaScript 上卷 笔记7】javaScript中对象的[[Prototype]]机制

    [[Prototype]]机制 [[Prototype]]是对象内部的隐试属性,指向一个内部的链接,这个链接的作用是:如果在对象上没有找到需要的属性或者方法引用,引擎就 会继续在 [[Prototyp ...

  7. javascript中的对象之间继承关系

    相信每个学习过其他语言的同学再去学习JavaScript时就会感觉到诸多的不适应,这真是一个颠覆我们以前的编程思想的一门语言,先不要说它的各种数据类型以及表达式的不同了,最让我们头疼,恐怕就是面向对象 ...

  8. javascript中 __proto__与prorotype的理解

    我们先看看这样一段代码: <script type="text/javascript"> var Person = function () { }; var p = n ...

  9. 再说javascript 的__proto__ 和prototype 属性

    过了一段时间,没写 原生的 javascript 的了,感觉天天在用框架写代码,框架写代码完全限定死了你所需要思考的东西,只是在处理一些业务逻辑,真正的代码 都感觉不会写了. 突然发现,框架用的不熟悉 ...

随机推荐

  1. NSDate获取当前时区的时间

    [NSDate date]获取的是GMT时间,要想获得某个时区的时间,以下代码可以解决这个问题 NSDate *date = [NSDate date]; NSTimeZone *zone = [NS ...

  2. 关于.net编译时目标生成平台

    x86: 将程序集编译为由兼容 x86 的 32 位公共语言运行库运行. x64: 将程序集编译为由支持 AMD64 或 EM64T 指令集的计算机上的 64 位公共语言运行库运行. anycpu:( ...

  3. css回忆(一)

    1.css的引入方式: a) 在head部分加入<link  rel="stylesheet" type="text/css" href="my ...

  4. SQL GROUP BY 后排序

    由于GROUP BY 使用Sum函数后 ID等唯一值就无法查询出来了,所以想按照ID排序也就不可以了. 这时可以使用一个MIN 或者MAX函数来取得一个最小或者最大的ID 这样就可以实现以其中一条ID ...

  5. JavaIO总结

    Java IO流分为字节流和字符流 下面首先介绍一下字节流 /** * 字节流测试 * @author hc * */ public class Test { public static void m ...

  6. Oracle使用经验总结

    oracle数据库是一种大型数据库系统,一般应用于商业,政府部门,它的功能很强大,能够处理大批量的数据,在网络方面也用的非常多.Oracle数据库管理系统是一个以关系型和面向对象为中心管理数据的数据库 ...

  7. 5----table类型

    table类型是非常重要的Lua数据类型,也是Lua唯一能描述数据结构的类型 table类型可以很灵活的描述多种数据结构,其本身是基于键值对的形式存储数据的 字典结构 字典结构的table 的两种创建 ...

  8. 深入浅出设计模式——适配器模式(Adapter Pattern)

    模式动机 在软件开发中采用类似于电源适配器的设计和编码技巧被称为适配器模式. 通常情况下,客户端可以通过目标类的接口访问它所提供的服务.有时,现有的类可以满足客户类的功能需要,但是它所提供的接口不一定 ...

  9. 《BI项目笔记》用Excel2013连接和浏览OLAP多维数据集

    用Excel2013连接和浏览OLAP多维数据集

  10. nodejs的第一天学习笔记

    一. js的模块化 什么是模块化: 模块化的概念最早是后台,随着ajax技术的兴起,js在编程中所占的地位越来越高,同时js的文件也相应的越来越多.为了方便文件的管理和更新,提出了js文件的模块 化, ...