Javascript中的Prototype到底是什么
Javascript也是面向?qū)ο蟮恼Z言,但它是一種基于原型Prototype的語言,而不是基于類的語言。在Javascript中,類和對象看起來沒有太多的區(qū)別。
什么是prototype:
function定義的對象有一個prototype屬性,prototype屬性又指向了一個prototype對象,注意prototype屬性與prototype對象是兩個不同的東西,要注意區(qū)別。在prototype對象中又有一個constructor屬性,這個constructor屬性同樣指向一個constructor對象,而這個constructor對象恰恰就是這個function函數(shù)本身。 是不是很繞?用偽代碼表示如下:
var function{ prototype:prototype{ constructor:constructor == function } }
還不明白?看圖吧:
prototype的作用:
這個prototype到底有什么作用呢?看下面的例子:
function jb51(){ } jb51.prototype.name = "a"; var test = new jb51(); alert(test.name)//"a";
奇怪吧,明明沒有為test設(shè)置name屬性,可是為什么會有值?
這就是prototype的功勞了,uw3c中prototype屬性中的name對象,在uw3c被new構(gòu)造函數(shù)之后,被繼承到了對象test的屬性中。接著看:
var name = "js"; function jb51(name){ alert(this.name);//"css" } jb51.prototype.name = "css"; var test = new jb51(); test()
為什么alert的值不是“js”?這個過程大致如下:
var test={}; uw3c.call(test);
第一步是建立一個新對象(test)。
第二步將該對象(test)內(nèi)置的原型對象設(shè)置為構(gòu)造函數(shù)(就是uw3c)prototype 屬性引用的那個原型對象。
第三步就是將該對象(test)作為this 參數(shù)調(diào)用構(gòu)造函數(shù)(就是uw3c),完成成員設(shè)置等初始化工作。
其中第二步中出現(xiàn)了一個新名詞就是內(nèi)置的原型對象,注意這個新名詞跟prototype對象不是一回事, 為了區(qū)別我叫它inobj,inobj就指向了函數(shù)uw3c的prototype對象。在uw3c的prototype對象中出現(xiàn)的任何屬性或者函數(shù)都可以在test對象中直接使用,這個就是JS中的原型繼承了。
通常,這樣創(chuàng)建一個對象:
function person(name){ this.sayHi = function(){ alert('hi ' + this.name); } this.name = name; } var p = new person("dan"); p.sayHi();
以上,使用new關(guān)鍵字,通過對象(函數(shù)也是特殊對象)創(chuàng)建一個對象實例。
在基于類的語言中,屬性或字段通常都是在類中事先定義好了,但在Javascript中,在創(chuàng)建對象之后還可以為類添加字段。
function animal(){} var cat = new animal(); cat.color = "green";
以上,color這個字段只屬于當(dāng)前的cat實例。
對于后加的字段,如果想讓animal的所有實例都擁有呢?
--使用Prototype function animal(){} animal.prototype.color= "green"; var cat = new animal(); var dog = new animal(); console.log(cat.color);//green console.log(dog.color);//green
通過Prototype不僅可以添加字段,還可以添加方法。
function animal(){} animal.prototype.color= "green"; var cat = new animal(); var dog = new animal(); console.log(cat.color);//green console.log(dog.color);//green animal.prototype.run = funciton(){ console.log("run"); } dog.run();
原來通過prototype屬性,在創(chuàng)建對象之后還可以改變對象的行為。
比如,可以為數(shù)組這個特殊對象添加一個方法。
Array.prototype.remove = function(elem){ var index = this.indexof(elem); if(index >= 0){ this.splice(index, 1); } } var arr = [1, 2, 3] ; arr.remove(2);
除了通過prototype為對象定義屬性或方法,還可以通過對象的構(gòu)造函數(shù)來定義類的屬性或方法。
function animal(){ this.color = "green"; this.run = function(){ console.log("run"); } } var mouse = new animal(); mouse.run();
以上做法的也可以讓所有的animal實例共享所有的字段和方法。并且還有一個好處是可以在構(gòu)造函數(shù)中使用類的局部變量。
function animal(){ var runAlready = false; this.color = "green"; this.run = funciton(){ if(!runAlreadh){ console.log("start running"); } else { console.log("already running") } } }
其實,一個更加實際的做法是把通過構(gòu)造函數(shù)結(jié)合通過prototype定義一個類的字段和行為。
function animal(){ var runAlready = false; this.run = function(){ if(!runAlready){ console.log('i am running'); } else { console.log("i am already running"); } } } animal.prototype.color = ''; animal.prototype.hide = funciton(){ console.log(""); } var horse = new animal(); horse.run(); horse.hide();
Prototype允許我們在創(chuàng)建對象之后來改變對象或類的行為,并且這些通過prototype屬性添加的字段或方法所有對象實例是共享的。
- javascript 對象 與 prototype 原型用法實例分析
- 深入理解javascript prototype的相關(guān)知識
- js中的hasOwnProperty和isPrototypeOf方法使用實例
- JavaScript prototype 使用介紹
- js中prototype用法詳細(xì)介紹
- 詳解Javascript中prototype屬性(推薦)
- JS 面向?qū)ο笾衿娴膒rototype
- JS面向?qū)ο蟆rototype、call()、apply()
- 深入了解javascript中的prototype與繼承
- javascript prototype的深度探索不是原型繼承那么簡單
- js prototype深入理解及應(yīng)用實例分析
相關(guān)文章
小程序使用watch監(jiān)聽數(shù)據(jù)變化的方法詳解
這篇文章主要介紹了小程序使用watch監(jiān)聽數(shù)據(jù)變化的方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09JS實現(xiàn)的漢字與Unicode碼相互轉(zhuǎn)化功能分析
這篇文章主要介紹了JS實現(xiàn)的漢字與Unicode碼相互轉(zhuǎn)化功能,結(jié)合實例形式分析了javascript實現(xiàn)漢字與Unicode碼轉(zhuǎn)換相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-05-05localStorage的黑科技-js和css緩存機(jī)制
本文主要介紹了localStorage的黑科技-js和css緩存機(jī)制的相關(guān)知識,具有一定的參考價值,下面跟著小編一起來看下吧2017-02-02微信小程序自定義組件傳值 頁面和組件相互傳數(shù)據(jù)操作示例
這篇文章主要介紹了微信小程序自定義組件傳值 頁面和組件相互傳數(shù)據(jù)操作,結(jié)合實例形式分析了微信小程序常見傳值操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05