• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            對(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,<<<<<
            3.系統(tǒng)內(nèi)置Object,Function,Number,String,Boolean等f(wàn)unction對(duì)象
            typeof(Object)//function
            typeof(Function)//function
            typeof(Number)//function
            4.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ì)象,比較特別
            6.Number,String,Boolean
            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
            8.object對(duì)象只有constructor屬性而沒有prototype屬性(null什么都沒有)
            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
            delete(Object.prototype)
            typeof(Object.prototype)//object
            object.prototype=undefined;
            typeof(Object.prototype)//object,<<<<<<<不可刪
            typeof(Object.constructor)//function
            Object.constructor=undefined;
            typeof(Object.constructor)//undefined,可改,可刪
            typeof(Object.prototype)//object
            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í)收集.
            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ì)象被解除引用
            定義函數(shù)備用
            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:
            Object.x=1;
            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
            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
            Object.y=2;
            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
            Object.prototype.y=2;
            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被同步
            delete(Object.prototype.z);
            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:
            我的理解是:
            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:
            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
            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)象
            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
            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屬性,指向自己.
            可以如下解釋object和function之間的關(guān)系
            每個(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,被刪除
            解釋:
            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

            <2007年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 65
            • 文章 - 0
            • 評(píng)論 - 80
            • 引用 - 0

            常用鏈接

            留言簿(9)

            隨筆分類(67)

            隨筆檔案(65)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久91精品国产91久久小草| 国产成人精品久久综合| 伊人久久大香线蕉无码麻豆| 伊人久久大香线蕉AV一区二区| 亚洲午夜无码AV毛片久久| 久久青青草原亚洲av无码app| 日本免费一区二区久久人人澡| 亚洲国产精品成人久久蜜臀 | 久久精品国产亚洲一区二区三区| 久久99精品九九九久久婷婷 | 精品国产乱码久久久久久郑州公司 | 精品国产热久久久福利| 久久精品桃花综合| 国产精品久久久久久搜索| 国色天香久久久久久久小说| 久久精品国产久精国产一老狼| 国产亚洲婷婷香蕉久久精品 | 久久AV无码精品人妻糸列| 久久亚洲av无码精品浪潮| 97超级碰碰碰碰久久久久| 久久精品国产99国产精偷| 国产美女久久久| 国产亚洲色婷婷久久99精品91| 精品久久久久久无码中文野结衣| 狠狠色婷婷久久一区二区三区| 国产精品久久久99| 久久99热这里只有精品国产| 伊人久久大香线蕉av不变影院| 中文字幕久久久久人妻| 2022年国产精品久久久久| 99久久夜色精品国产网站| 久久毛片免费看一区二区三区| 婷婷久久久亚洲欧洲日产国码AV| 久久成人18免费网站| 久久午夜电影网| 人人狠狠综合久久亚洲88| 久久99国产精品尤物| 久久国产精品-国产精品| 国产一区二区精品久久| 99精品久久久久久久婷婷| 久久久久国产成人精品亚洲午夜|