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

javascript 中__proto__和prototype詳解

 更新時(shí)間:2014年11月25日 09:42:01   投稿:hebedich  
本文詳細(xì)介紹了javascript的內(nèi)部原型__proto__和構(gòu)造器原型prototype,以及他們之間的異同,十分的詳盡,有需要的小伙伴快來(lái)研究下吧。

__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ù),下面的問(wèn)題是:如何通過(guò)這個(gè)構(gòu)造器來(lái)創(chuàng)建一個(gè)對(duì)象呢?
答案: new

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

復(fù)制代碼 代碼如下:

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

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

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

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

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

這說(shuō)明什么呢?

所有的構(gòu)造器都來(lái)自于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ì)象。如下

復(fù)制代碼 代碼如下:

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__是誰(shuí)呢?

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

復(fù)制代碼 代碼如下:

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

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

最后Object.prototype的__proto__是誰(shuí)?

復(fù)制代碼 代碼如下:

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

已經(jīng)到頂了,為null。

大家是否對(duì)javascript 中__proto__和prototype之間的區(qū)別聯(lián)系有所了解了呢,有疑問(wèn)就留言,大家共同探討吧

相關(guān)文章

最新評(píng)論