每日前端一问--js中的_proto_和prototype的区别

js中的_proto_和prototype

首先指出_proto_并非全部浏览器都支持,每一个浏览器可能有不一样的实现,就是书中的[[property]]。浏览器

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

对于这一点可能会了解少一点,通常会首先会想到构造器/函数的prototype,指向该原型。直接上代码。app

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)固然ES6也有新增(Set,Map等),这里列举了可访问的8个构造器。剩下如Global不能直接访问,Arguments仅在函数调用时由JS引擎建立,Math,JSON是以对象形式存在的,无需new。它们的__proto__是Object.prototype。以下:函数

Math.__proto__ === Object.prototype  // true
JSON.__proto__ === Object.prototype  // true
复制代码

前面说的“全部构造器/函数”固然包括自定义的。以下:测试

// 函数声明
function MyObject() {}
// 函数表达式
var Man = function() {}
console.log(MyObject.__proto__ === Function.prototype) // true
console.log(Man.__proto__ === Function.prototype)    // true
复制代码

这就说明一点:全部的构造器都来自于Function.prototype,甚至包括根构造器Object及Function自身。全部构造器都继承了Function.prototype的属性及方法。如length、call、apply、bind(ES5)。ui

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

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) 下看看。spa

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

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

console.log(Function.prototype.__proto__ === Object.prototype) // true
复制代码

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

最后Object.prototype的__proto__是谁?

Object.prototype.__proto__ === null  // true
复制代码

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

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

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

function MyObject(name) {
    this.name = name
}
var obj = new Person('jack')
console.log(obj.__proto__ === MyObject.prototype) // true
复制代码

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

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

var p = {}
var __proto__ = Object.getPrototypeOf(p)
console.log(__proto__ === Object.prototype) // true
复制代码
相关文章
相关标签/搜索