對(duì)javascript面向?qū)ο蠹夹g(shù)的理解(轉(zhuǎn))
1.javascript是面向?qū)ο蟮慕忉屝驼Z(yǔ)言,它在解釋之前會(huì)進(jìn)行語(yǔ)法分析,如果有語(yǔ)法錯(cuò)誤,立即停止;然后再逐行執(zhí)行,應(yīng)該是語(yǔ)法制導(dǎo)解釋執(zhí)行吧.所有的變量都是指針,變量指向?qū)ο?/div>
2.系統(tǒng)中內(nèi)置object,function,number,string,boolean等類型
typeof({})//object
typeof(Object)//function
typeof(1)//number
typeof('1')//string
typeof(true)//boolean
typeof(null)//object,<<<<<
typeof({})//object
typeof(Object)//function
typeof(1)//number
typeof('1')//string
typeof(true)//boolean
typeof(null)//object,<<<<<
3.系統(tǒng)內(nèi)置Object,Function,Number,String,Boolean等f(wàn)unction對(duì)象
typeof(Object)//function
typeof(Function)//function
typeof(Number)//function
typeof(Object)//function
typeof(Function)//function
typeof(Number)//function
4.Object
a={}
a=Object()
a=new Object()
上面的語(yǔ)句等價(jià)
typeof(a)//object
a={}
a=Object()
a=new Object()
上面的語(yǔ)句等價(jià)
typeof(a)//object
5.Function
function f(){}
f=function(){}
f=Function()
f=new Function()
上面的語(yǔ)句等價(jià)
typeof(f)//function,注意new Function返回function對(duì)象,比較特別
function f(){}
f=function(){}
f=Function()
f=new Function()
上面的語(yǔ)句等價(jià)
typeof(f)//function,注意new Function返回function對(duì)象,比較特別
6.Number,String,Boolean
a=Number(1)//typeof(a)==number
a=new Number(1)//typeof(a)==object<<<<<<<<
類推
a=Number(1)//typeof(a)==number
a=new Number(1)//typeof(a)==object<<<<<<<<
類推
7.function對(duì)象都有prototype屬性和constructor屬性
typeof(Object.prototype)//object
typeof(Function.prototype)//function<<<<<<<<
typeof(Number.prototype)//object
function f(){}
typeof(f.prototype)//object
typeof(Object.prototype)//object
typeof(Function.prototype)//function<<<<<<<<
typeof(Number.prototype)//object
function f(){}
typeof(f.prototype)//object
8.object對(duì)象只有constructor屬性而沒有prototype屬性(null什么都沒有)
a={}
typeof(a.constructor)//function
typeof(a.prototype)//undefined
a={}
typeof(a.constructor)//function
typeof(a.prototype)//undefined
9.任何對(duì)象都可以增加,修改和刪除屬性,但內(nèi)置屬性有例外
a={}
a.x=1;
a.x=2;
delete(a.x)
delete(a)
typeof(a)//undefined
a={}
a.x=1;
a.x=2;
delete(a.x)
delete(a)
typeof(a)//undefined
delete(Object.prototype)
typeof(Object.prototype)//object
object.prototype=undefined;
typeof(Object.prototype)//object,<<<<<<<不可刪
typeof(Object.prototype)//object
object.prototype=undefined;
typeof(Object.prototype)//object,<<<<<<<不可刪
typeof(Object.constructor)//function
Object.constructor=undefined;
typeof(Object.constructor)//undefined,可改,可刪
Object.constructor=undefined;
typeof(Object.constructor)//undefined,可改,可刪
typeof(Object.prototype)//object
delete(Object)
typeof(Object)//undefined<<<<<<<<<<,可以刪除
a={};
typeof(a)//object,仍然是object對(duì)象
delete(Object)
typeof(Object)//undefined<<<<<<<<<<,可以刪除
a={};
typeof(a)//object,仍然是object對(duì)象
因此可以認(rèn)為只有Object.prototype才代表object類型,F(xiàn)unction.prototype才代表function類,等等.后面還有細(xì)述
10.在javascript中的賦值操作都是引用操作(對(duì)對(duì)象的引用)
所謂的引用就是每個(gè)對(duì)象都維持一個(gè)引用計(jì)數(shù)器,變量創(chuàng)建時(shí)引用計(jì)數(shù)為0.變量都是指針.當(dāng)改對(duì)象被賦值給某變量時(shí),增加對(duì)象的引用計(jì)數(shù),變量指向該對(duì)象.當(dāng)刪除該變量或改變變量的值時(shí),減少原對(duì)象的引用.如果對(duì)象的引用為0,由垃圾收集器(gc)定時(shí)收集.
所謂的引用就是每個(gè)對(duì)象都維持一個(gè)引用計(jì)數(shù)器,變量創(chuàng)建時(shí)引用計(jì)數(shù)為0.變量都是指針.當(dāng)改對(duì)象被賦值給某變量時(shí),增加對(duì)象的引用計(jì)數(shù),變量指向該對(duì)象.當(dāng)刪除該變量或改變變量的值時(shí),減少原對(duì)象的引用.如果對(duì)象的引用為0,由垃圾收集器(gc)定時(shí)收集.
a={}//創(chuàng)建空對(duì)象,然后a對(duì)其引用
a.x=1;//創(chuàng)建對(duì)象Number(1),然后a.x對(duì)其引用
b=a;//b對(duì)a對(duì)象引用,即都指向前面的空對(duì)象
b.x=2;
alert(a.x)//=2,前面的Number(1)對(duì)象被解除引用
a.x=1;//創(chuàng)建對(duì)象Number(1),然后a.x對(duì)其引用
b=a;//b對(duì)a對(duì)象引用,即都指向前面的空對(duì)象
b.x=2;
alert(a.x)//=2,前面的Number(1)對(duì)象被解除引用
定義函數(shù)備用
function show(msg,obj)
{
document.writeln(msg);
for(var p in obj)
document.writeln(p+'->'+obj[p]+',');
document.writeln('<br>');
}
function show(msg,obj)
{
document.writeln(msg);
for(var p in obj)
document.writeln(p+'->'+obj[p]+',');
document.writeln('<br>');
}
11.研究Object這個(gè)function對(duì)象
show('Object:',Object);//Object:
show('Object.prototype:',Object.prototype);//Object.prototype:
show('Object:',Object);//Object:
show('Object.prototype:',Object.prototype);//Object.prototype:
Object.x=1;
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype:
//在Object的增加屬性不影響Object.prototype
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype:
//在Object的增加屬性不影響Object.prototype
Object.prototype.y=1;
show('Object:',Object);//Object: y->1, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//在Object.prototype中增加屬性影響Object
show('Object:',Object);//Object: y->1, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//在Object.prototype中增加屬性影響Object
Object.prototype.y=2;
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//對(duì)Object.prototype的屬性修改影響Object
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//對(duì)Object.prototype的屬性修改影響Object
Object.y=2;
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//對(duì)Object中屬性的修改不影響Object.prototype
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//對(duì)Object中屬性的修改不影響Object.prototype
delete(Object.y);
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//刪除Object的屬性不影響Object.prototype
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->1,
//刪除Object的屬性不影響Object.prototype
Object.prototype.y=2;
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//Object的屬性沒有改變
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//Object的屬性沒有改變
Object.prototype.z=2;
show('Object:',Object);//Object: z->2, y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2, z->2,
//可見在Object.prototype中增加了一個(gè)z屬性,Object被同步
show('Object:',Object);//Object: z->2, y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2, z->2,
//可見在Object.prototype中增加了一個(gè)z屬性,Object被同步
delete(Object.prototype.z);
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//屬性同時(shí)被刪除
show('Object:',Object);//Object: y->2, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: y->2,
//屬性同時(shí)被刪除
Object.y=2;
delete(Object.prototype.y);//Object: y->2, x->1,
show('Object:',Object);
show('Object.prototype:',Object.prototype);//Object.prototype:
delete(Object.prototype.y);//Object: y->2, x->1,
show('Object:',Object);
show('Object.prototype:',Object.prototype);//Object.prototype:
我的理解是:
Object是function對(duì)象,它繼承自object類型.它有自己的屬性,同時(shí)也從Object.prototype(前面說(shuō)過(guò)它才是object的代表)繼承屬性.
每個(gè)屬性都有一個(gè)是否被修改標(biāo)志.
.在Object.prototype中增加一個(gè)屬性,會(huì)導(dǎo)致Object的屬性被同步
如果該對(duì)象沒有該屬性,復(fù)制一份,指向同一個(gè)值,置未修改標(biāo)志;如果該對(duì)象有該屬性,不執(zhí)行任何操作
.修改Object.prototype中任何一個(gè)屬性,自然會(huì)導(dǎo)致Object的未修改繼承屬性同時(shí)改變
.刪除Object.prototype中任何一個(gè)屬性,會(huì)導(dǎo)致Object的未修改繼承屬性被同步刪除
.對(duì)Object的自身的屬性的增加,修改,刪除自然不會(huì)影響到Object.prototype,增加和修改會(huì)置修改標(biāo)志
一句話,Object.prototype的級(jí)別比Object高
Object是function對(duì)象,它繼承自object類型.它有自己的屬性,同時(shí)也從Object.prototype(前面說(shuō)過(guò)它才是object的代表)繼承屬性.
每個(gè)屬性都有一個(gè)是否被修改標(biāo)志.
.在Object.prototype中增加一個(gè)屬性,會(huì)導(dǎo)致Object的屬性被同步
如果該對(duì)象沒有該屬性,復(fù)制一份,指向同一個(gè)值,置未修改標(biāo)志;如果該對(duì)象有該屬性,不執(zhí)行任何操作
.修改Object.prototype中任何一個(gè)屬性,自然會(huì)導(dǎo)致Object的未修改繼承屬性同時(shí)改變
.刪除Object.prototype中任何一個(gè)屬性,會(huì)導(dǎo)致Object的未修改繼承屬性被同步刪除
.對(duì)Object的自身的屬性的增加,修改,刪除自然不會(huì)影響到Object.prototype,增加和修改會(huì)置修改標(biāo)志
一句話,Object.prototype的級(jí)別比Object高
11.研究Function和Object之間的關(guān)系
show('Object:',Object);//Object:
show('Object.prototype:',Object.prototype);//Object.prototype:
show('Function:',Function);//Function:
show('Function.prototype:',Function.prototype);//Function.prototype:
show('Object:',Object);//Object:
show('Object.prototype:',Object.prototype);//Object.prototype:
show('Function:',Function);//Function:
show('Function.prototype:',Function.prototype);//Function.prototype:
Object.prototype.x=1;
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: x->1,
show('Function:',Function);//Function: x->1,
show('Function.prototype:',Function.prototype);//Function.prototype: x->1,
//Object,Function,Function.prototype都有了屬性x
show('Object:',Object);//Object: x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: x->1,
show('Function:',Function);//Function: x->1,
show('Function.prototype:',Function.prototype);//Function.prototype: x->1,
//Object,Function,Function.prototype都有了屬性x
Function.prototype.y=1;
show('Object:',Object);//Object: y->1, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: x->1,
show('Function:',Function);//Function: y->1, x->1,
show('Function.prototype:',Function.prototype);//Function.prototype: x->1, y->1
//Object,Function都有了屬性y,而Object.prototype不受影響,
//可見Object.prototype比Function.prototype級(jí)別高
//Object,Function都是function類型(由Function.prototype代表)的對(duì)象
//這里的屬性也有類似上面的同步現(xiàn)象
show('Object:',Object);//Object: y->1, x->1,
show('Object.prototype:',Object.prototype);//Object.prototype: x->1,
show('Function:',Function);//Function: y->1, x->1,
show('Function.prototype:',Function.prototype);//Function.prototype: x->1, y->1
//Object,Function都有了屬性y,而Object.prototype不受影響,
//可見Object.prototype比Function.prototype級(jí)別高
//Object,Function都是function類型(由Function.prototype代表)的對(duì)象
//這里的屬性也有類似上面的同步現(xiàn)象
alert(Object);//function Object(){[native code]}
alert(Object.prototype);//object
alert(Object.prototype.prototype);//undefined,object無(wú)prototype屬性
alert(Object.prototype.constructor);//function Object(){[native code]},由Object構(gòu)造
alert(Object.constructor);//function Function(){[native code]}
alert(Function);//function Function(){[native code]}
alert(Function.prototype);//function prototype(){[native code]} <<<<<<<<<<<
alert(Function.prototype.prototype);//undefined<<<<<<<<<<,這個(gè)function無(wú)prototype屬性
alert(Function.prototype.constructor);//function Function(){[native code]}由Function構(gòu)造
alert(Function.constructor);//function Function(){[native code]},自己構(gòu)造自己
//可以看到這里面有遞歸引用關(guān)系
O.P
| \
| F.P
\ / \
O F
alert(Object.prototype);//object
alert(Object.prototype.prototype);//undefined,object無(wú)prototype屬性
alert(Object.prototype.constructor);//function Object(){[native code]},由Object構(gòu)造
alert(Object.constructor);//function Function(){[native code]}
alert(Function);//function Function(){[native code]}
alert(Function.prototype);//function prototype(){[native code]} <<<<<<<<<<<
alert(Function.prototype.prototype);//undefined<<<<<<<<<<,這個(gè)function無(wú)prototype屬性
alert(Function.prototype.constructor);//function Function(){[native code]}由Function構(gòu)造
alert(Function.constructor);//function Function(){[native code]},自己構(gòu)造自己
//可以看到這里面有遞歸引用關(guān)系
O.P
| \
| F.P
\ / \
O F
Function.prototype.f=function(){};Function.f.f.f.f.f....一直下去的引用都有效
具體的操作過(guò)程為,根據(jù)object和function創(chuàng)建一個(gè)function對(duì)象,然后把它賦值給Function.prototype,
然后同步所有function對(duì)象(Function.prototype已經(jīng)被更新,排除),導(dǎo)致在該function對(duì)象中增加一個(gè)f屬性,指向自己.
具體的操作過(guò)程為,根據(jù)object和function創(chuàng)建一個(gè)function對(duì)象,然后把它賦值給Function.prototype,
然后同步所有function對(duì)象(Function.prototype已經(jīng)被更新,排除),導(dǎo)致在該function對(duì)象中增加一個(gè)f屬性,指向自己.
可以如下解釋object和function之間的關(guān)系
每個(gè)對(duì)象都是object的實(shí)例,每個(gè)函數(shù)對(duì)象又同時(shí)都是function的實(shí)例.通過(guò)Object.prototype可以引用到
object,通過(guò)Function.prototype可以引用到function.
每個(gè)對(duì)象都是object的實(shí)例,每個(gè)函數(shù)對(duì)象又同時(shí)都是function的實(shí)例.通過(guò)Object.prototype可以引用到
object,通過(guò)Function.prototype可以引用到function.
12.new操作
Object.prototype.x=1;
Function.prototype.y=1;
function f(){alert(this.x);}
alert(f.constructor);//function Function(){[native code]}由Function構(gòu)造
alert(f.prototype.constructor);//function f(){},指向自己
f.prototype.z=1;
show('f:',f);//f: y->1, x->1, 既繼承object又繼承funciton
show('f.prototype:',f.prototype);//f.prototype: x->1, z->1, 繼承自object
a=new f();//alert(1)
show('a:',a);//a: z->1, x->1, 既繼承f.prototype,又繼承object
delete(f.prototype);//可刪
b=new f();
show('b:',b);//b: x->1,
alert(f.prototype);//undefined,被刪除
Object.prototype.x=1;
Function.prototype.y=1;
function f(){alert(this.x);}
alert(f.constructor);//function Function(){[native code]}由Function構(gòu)造
alert(f.prototype.constructor);//function f(){},指向自己
f.prototype.z=1;
show('f:',f);//f: y->1, x->1, 既繼承object又繼承funciton
show('f.prototype:',f.prototype);//f.prototype: x->1, z->1, 繼承自object
a=new f();//alert(1)
show('a:',a);//a: z->1, x->1, 既繼承f.prototype,又繼承object
delete(f.prototype);//可刪
b=new f();
show('b:',b);//b: x->1,
alert(f.prototype);//undefined,被刪除
解釋:
new先創(chuàng)建一空對(duì)象,然后同步object和f.prototype中的屬性,接著讓this指針指向該對(duì)象,執(zhí)行函數(shù)f的代碼,最后返回該對(duì)象
a={};等價(jià)于a=new Object();object==Object.prototype,因此只使用一次object同步
new先創(chuàng)建一空對(duì)象,然后同步object和f.prototype中的屬性,接著讓this指針指向該對(duì)象,執(zhí)行函數(shù)f的代碼,最后返回該對(duì)象
a={};等價(jià)于a=new Object();object==Object.prototype,因此只使用一次object同步
posted on 2007-04-05 14:38 PeakGao 閱讀(456) 評(píng)論(0) 編輯 收藏 引用 所屬分類: Javascript
只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。 | ||
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
![]() |
||
相關(guān)文章:
|
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|