欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入淺析JavaScript中prototype和proto的關(guān)系

 更新時(shí)間:2015年11月15日 11:51:14   投稿:mrr  
prototype,每一個(gè)函數(shù)對(duì)象都有一個(gè)顯示的prototype屬性,而proto每個(gè)對(duì)象都有一個(gè)名為_proto_內(nèi)部隱藏屬性。本文給大家介紹JavaScript中prototype和proto的關(guān)系,需要的朋友參考下

prototype,每一個(gè)函數(shù)對(duì)象都有一個(gè)顯示的prototype屬性,它代表了對(duì)象的原型(Function.prototype函數(shù)對(duì)象是個(gè)例外,沒有prototype屬性)。

__proto__:每個(gè)對(duì)象都有一個(gè)名為__proto__的內(nèi)部隱藏屬性,指向于它所對(duì)應(yīng)的原型對(duì)象(chrome、firefox中名稱為__proto__,并且可以被訪問到)。原型鏈正是基于__proto__才得以形成

(note:不是基于函數(shù)對(duì)象的屬性prototype)。

簡單的說:__proto__是內(nèi)部原型,prototype是構(gòu)造器原型(構(gòu)造器其實(shí)就是函數(shù))

構(gòu)造器的原型(prototype)是一個(gè)對(duì)象

那什么是構(gòu)造器呢?

要想創(chuàng)建一個(gè)對(duì)象,首先要有一個(gè)對(duì)象構(gòu)造器,就像php里面一樣,要想創(chuàng)建一個(gè)對(duì)象,首先要有一個(gè)類
構(gòu)造器的實(shí)質(zhì)就是一個(gè)函數(shù),下面的問題是:如何通過這個(gè)構(gòu)造器來創(chuàng)建一個(gè)對(duì)象呢?

答案: new

構(gòu)造器構(gòu)造的是對(duì)象。

一、所有構(gòu)造器/函數(shù)的__proto__都指向Function.prototype,它是一個(gè)空函數(shù)(Empty function)

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

說明了Number等都是構(gòu)造器,這些構(gòu)造器其實(shí)是Function的一個(gè)對(duì)象。 也就是說相當(dāng)于 var Number = new Function();

JavaScript中有內(nèi)置(build-in)構(gòu)造器/對(duì)象共計(jì)12個(gè)(ES5中新加了JSON),這里列舉了可訪問的8個(gè)構(gòu)造器。剩下如Global不能直接訪問,Arguments僅在函數(shù)調(diào)用時(shí)由JS引擎創(chuàng)建,Math,JSON是以對(duì)象形式存在的,無需new。它們的__proto__是Object.prototype。如下

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

上面說的“所有構(gòu)造器/函數(shù)”當(dāng)然包括自定義的。如下

// 函數(shù)聲明
function Person()
{}
// 函數(shù)表達(dá)式
var Man
=
function()
{}
console.log(Person.__proto__ === Function.prototype)
// true
console.log(Man.__proto__ ===
Function.prototype)   
// true

這說明什么呢?

所有的構(gòu)造器都來自于Function.prototype,甚至包括根構(gòu)造器Object及Function自身。所有構(gòu)造器都繼承了Function.prototype的屬性及方法。如length、call、apply、bind(ES5)。

Function.prototype也是唯一一個(gè)typeof XXX.prototype為 “function”的prototype。其它的構(gòu)造器的prototype都是一個(gè)對(duì)象。如下

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

噢,上面還提到它是一個(gè)空的函數(shù),alert(Function.prototype) 下看看。

知道了所有構(gòu)造器(含內(nèi)置及自定義)的__proto__都是Function.prototype,那Function.prototype的__proto__是誰呢?

相信都聽說過JavaScript中函數(shù)也是一等公民,那從哪能體現(xiàn)呢?如下

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

這說明所有的構(gòu)造器也都是一個(gè)普通JS對(duì)象,可以給構(gòu)造器添加/刪除屬性等。同時(shí)它也繼承了Object.prototype上的所有方法:toString、valueOf、hasOwnProperty等。

最后Object.prototype的__proto__是誰?

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

下面給大家分享一個(gè)Function、Object、Prototype、__proto__內(nèi)存關(guān)系圖

相關(guān)文章

最新評(píng)論