最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript 中__proto__和prototype詳解

 更新時(shí)間:2014年11月25日 09:42:01   投稿:hebedich  
本文詳細(xì)介紹了javascript的內(nèi)部原型__proto__和構(gòu)造器原型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)

復(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

說明了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。如下

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

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

上面說的“所有構(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

這說明什么呢?

所有的構(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ì)象。如下

復(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__是誰呢?

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

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

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

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

最后Object.prototype的__proto__是誰?

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

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

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

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

相關(guān)文章

最新評(píng)論

池州市| 于田县| 洛隆县| 邵东县| 修水县| 平湖市| 富顺县| 大理市| 健康| 治县。| 丹寨县| 永定县| 科技| 德清县| 沁阳市| 广州市| 青神县| 盐源县| 工布江达县| 贺兰县| 都昌县| 绥滨县| 东乡| 万全县| 阿鲁科尔沁旗| 茶陵县| 淮阳县| 闻喜县| 北碚区| 通山县| 明光市| 华容县| 搜索| 石嘴山市| 天全县| 额尔古纳市| 太谷县| 龙川县| 玉屏| 昌平区| 兴国县|