javascript prototype,executing,context,closure
更新時(shí)間:2008年12月24日 02:29:17 作者:
JavaScript是一種靈活的腳本語(yǔ)言。與Java、C++等需要編譯執(zhí)行的程序設(shè)計(jì)語(yǔ)言有很大不同,在使用的過(guò)程中,會(huì)暴露出一些問(wèn)題。
要學(xué)好JavaScript,有幾個(gè)基本概念必須搞清楚:prototype,executing,context,closure。
Prototype
在JavaScript語(yǔ)言中,通常使用Prototype來(lái)實(shí)現(xiàn)OO。在這里,我們不對(duì)JavaScript的OO實(shí)現(xiàn)進(jìn)行過(guò)多的探討,著重來(lái)看一下JS中對(duì)象的內(nèi)存模型。在開(kāi)始之前,需要先明確以下幾點(diǎn):
1. JS中,存在以下幾種數(shù)據(jù)類(lèi)型:string,number,boolean,object,function(注意:首字母均為小寫(xiě))。
2 “Object”, “String”, “Date”等內(nèi)置數(shù)據(jù)類(lèi)型,在JS中實(shí)際上是函數(shù)名稱(chēng)(使用"alert(typeof Object)"可以驗(yàn)證,輸出為"function")。我們通常指的類(lèi)型為"Date"的數(shù)據(jù)類(lèi)型,實(shí)際上是通過(guò)"new Date"所產(chǎn)生的對(duì)象。
3. 在JavaScript中,對(duì)象都是associative array (hash table),可以動(dòng)態(tài)指定對(duì)象的property。
4. 在Firefox中可以使用"__proto__"屬性來(lái)查看一個(gè)對(duì)象的"prototype"。
下面我們來(lái)看一個(gè)簡(jiǎn)單的例子:
function Person() { this.age = 10; this.name = "test";}Person.prototype = new Object;var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"
可以看出Person數(shù)據(jù)類(lèi)型具有一個(gè)“prototype”,如果更改這個(gè)prototype,會(huì)影響到所有已經(jīng)產(chǎn)生的Person類(lèi)型的對(duì)象,同時(shí)也會(huì)影響到以后建立的Person類(lèi)型的對(duì)象。如果指定一個(gè)function的prototype屬性,則所有使用該function生成的對(duì)象實(shí)例中(使用new操作符)都具有該prototype,在Firefox 中,可以使用"__proto__"屬性訪問(wèn)。
通常情況下,我們講JS中的對(duì)象都繼承Object數(shù)據(jù)類(lèi)型,這是如何體現(xiàn)的呢?我們把以上的程序稍微修改一下:
function Person() { this.age = 10; this.name = "test";}var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"alert(p.__proto__.__proto__); // output "[object Object]"alert(p.__proto__.__proto__ == Object.prototype); // output "true"alert(p.__proto__.__proto__.__proto__); // output "null"
由以上程序可以看到,Person的"prototype"(在這里,沒(méi)有明確指定Person.prototype, 而是使用缺省值)的"prototype" (p.__proto__.__proto__)正是Object.prototype, Object.prototype是prototype chain的終點(diǎn)(其自己的祖先為null)。
在JS中,Object是function,同時(shí),所有function的實(shí)例,也都是Object。請(qǐng)看如下程序:
/* Object, Function都是function數(shù)據(jù)類(lèi)型 */alert(typeof Object); // output "function"alert(typeof Function); // output "function"/* Function的prototype是一個(gè)空f(shuō)unction */alert(Function.prototype); // output "function() {}"alert(Function.__proto__ == Function.prototype); // output "true"/* function是object, 其prototype chain的終點(diǎn)是Object.prototype */alert(Function.__proto__.__proto__ == Object.prototype); //output "true"/* Object是function的實(shí)例 */ alert(Object.__proto__ == Function.prototype); // output "true"alert(Object.__proto__.__proto__ == Object.prototype); // output "true"改變Function.prototype會(huì)影響到“Object”,改變Object.prototype會(huì)影響到所有function的實(shí)例。
Prototype
在JavaScript語(yǔ)言中,通常使用Prototype來(lái)實(shí)現(xiàn)OO。在這里,我們不對(duì)JavaScript的OO實(shí)現(xiàn)進(jìn)行過(guò)多的探討,著重來(lái)看一下JS中對(duì)象的內(nèi)存模型。在開(kāi)始之前,需要先明確以下幾點(diǎn):
1. JS中,存在以下幾種數(shù)據(jù)類(lèi)型:string,number,boolean,object,function(注意:首字母均為小寫(xiě))。
2 “Object”, “String”, “Date”等內(nèi)置數(shù)據(jù)類(lèi)型,在JS中實(shí)際上是函數(shù)名稱(chēng)(使用"alert(typeof Object)"可以驗(yàn)證,輸出為"function")。我們通常指的類(lèi)型為"Date"的數(shù)據(jù)類(lèi)型,實(shí)際上是通過(guò)"new Date"所產(chǎn)生的對(duì)象。
3. 在JavaScript中,對(duì)象都是associative array (hash table),可以動(dòng)態(tài)指定對(duì)象的property。
4. 在Firefox中可以使用"__proto__"屬性來(lái)查看一個(gè)對(duì)象的"prototype"。
下面我們來(lái)看一個(gè)簡(jiǎn)單的例子:
function Person() { this.age = 10; this.name = "test";}Person.prototype = new Object;var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"
可以看出Person數(shù)據(jù)類(lèi)型具有一個(gè)“prototype”,如果更改這個(gè)prototype,會(huì)影響到所有已經(jīng)產(chǎn)生的Person類(lèi)型的對(duì)象,同時(shí)也會(huì)影響到以后建立的Person類(lèi)型的對(duì)象。如果指定一個(gè)function的prototype屬性,則所有使用該function生成的對(duì)象實(shí)例中(使用new操作符)都具有該prototype,在Firefox 中,可以使用"__proto__"屬性訪問(wèn)。
通常情況下,我們講JS中的對(duì)象都繼承Object數(shù)據(jù)類(lèi)型,這是如何體現(xiàn)的呢?我們把以上的程序稍微修改一下:
function Person() { this.age = 10; this.name = "test";}var p = new Person;alert(p); // output "[object Object]"alert(p.__proto__); // output "[object Object]"alert(p.__proto__.__proto__); // output "[object Object]"alert(p.__proto__.__proto__ == Object.prototype); // output "true"alert(p.__proto__.__proto__.__proto__); // output "null"
由以上程序可以看到,Person的"prototype"(在這里,沒(méi)有明確指定Person.prototype, 而是使用缺省值)的"prototype" (p.__proto__.__proto__)正是Object.prototype, Object.prototype是prototype chain的終點(diǎn)(其自己的祖先為null)。
在JS中,Object是function,同時(shí),所有function的實(shí)例,也都是Object。請(qǐng)看如下程序:
/* Object, Function都是function數(shù)據(jù)類(lèi)型 */alert(typeof Object); // output "function"alert(typeof Function); // output "function"/* Function的prototype是一個(gè)空f(shuō)unction */alert(Function.prototype); // output "function() {}"alert(Function.__proto__ == Function.prototype); // output "true"/* function是object, 其prototype chain的終點(diǎn)是Object.prototype */alert(Function.__proto__.__proto__ == Object.prototype); //output "true"/* Object是function的實(shí)例 */ alert(Object.__proto__ == Function.prototype); // output "true"alert(Object.__proto__.__proto__ == Object.prototype); // output "true"改變Function.prototype會(huì)影響到“Object”,改變Object.prototype會(huì)影響到所有function的實(shí)例。
您可能感興趣的文章:
- javascript 中String.match()與RegExp.exec()的區(qū)別說(shuō)明
- Javascript中使用exec進(jìn)行正則表達(dá)式全局匹配時(shí)的注意事項(xiàng)
- javascript document.execCommand() 常用解析
- js正則表達(dá)式test()和exec()用法實(shí)例
- js正則表達(dá)式中test,exec,match方法的區(qū)別說(shuō)明
- js正則表達(dá)exec與match的區(qū)別說(shuō)明
- js正則表達(dá)式之exec方法講解
- JS正則中的match與exec使用說(shuō)明
- JavaScript中exec函數(shù)用法實(shí)例分析
相關(guān)文章
Prototype源碼淺析 String部分(一)之有關(guān)indexOf優(yōu)化
Prototype源碼淺析 String部分(一)之有關(guān)indexOf優(yōu)化介紹,需要的朋友可以參考下。2012-01-01[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦
[轉(zhuǎn)]prototype 源碼解讀 超強(qiáng)推薦...2007-02-02Prototype Array對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象擴(kuò)展了JS原生的Array對(duì)象,提供了一些基本的工具函數(shù),有些方法非常簡(jiǎn)單,源碼里就不在注釋了。2009-07-07JavaScript語(yǔ)法著色引擎(demo及打包文件下載)
JavaScript語(yǔ)法著色引擎(demo及打包文件下載)...2007-06-06JavaScript使用prototype定義對(duì)象類(lèi)型
JavaScript使用prototype定義對(duì)象類(lèi)型...2007-02-02Prototype Date對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象里面就一個(gè)toJSON方法,非常簡(jiǎn)單2009-07-07