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

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

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

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

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

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

簡(jiǎn)單的說(shuō):__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)

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。如下

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

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

這說(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ì)象。如下

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)呢?如下

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í)?

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

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

相關(guān)文章

最新評(píng)論

沁源县| 新竹市| 广平县| 呼伦贝尔市| 北川| 淮滨县| 新营市| 郑州市| 灵丘县| 石林| 浏阳市| 韩城市| 通渭县| 祁东县| 阳朔县| 凉城县| 尼玛县| 龙州县| 肇东市| 宝丰县| 肥城市| 莱阳市| 莒南县| 台北县| 贵阳市| 饶阳县| 塔城市| 福鼎市| 天峨县| 星座| 稻城县| 保定市| 耒阳市| 罗甸县| 唐海县| 二手房| 二连浩特市| 廉江市| 翼城县| 礼泉县| 错那县|