日期:2014-05-16  浏览次数:20292 次

JS var self =this;
var self = this //鎸囦唬鏈綔鐢ㄥ煙鐨勫璞?

鎴戜滑缁欏嚭涓€涓鏈夌殑 self 鍙傛暟銆傝繖涓彲浠ヤ护瀵硅薄瀵圭鏈夋柟娉曞彲瑙併€傝繖绉嶅仛娉曟槸鍥犱负绉佹湁鏂规硶鏃犳硶琚叕鍏辨柟娉曡皟鐢ㄣ€傝浠ょ鏈夋柟娉曟湁鐢?
瀹冧滑琚檮鍔犲埌浜嗗璞′笂锛屼絾瀹冧滑鏃犳硶浠庡閮ㄨ闂紝鍚屾椂瀹冧滑涔熸棤娉曡杩欎釜瀵硅薄鐨勫叕鍏辨柟娉?prototype鏂规硶閮芥槸鍏湁鐨?鎵€璁块棶銆備粬浠彧瀵圭鏈夋垚鍛樺彲瑙併€傜鏈夋柟娉曞垯鏄瀯閫犲櫒鍐呴儴鐨勫嚱鏁般€侲CMAScript Language Specification涓殑涓€涓敊璇紝杩欎釜閿欒浠?this 涓嶈兘姝g‘鍦板鍐呴儴鍑芥暟璁剧疆銆?


Code
 function Class1(){
            //self(self琚檮鍔犲埌浜嗗璞′笂) self鍙绉佹湁鎴愬憳鍙(鑳?鐐瑰嚭鏉?i aa() .鐐逛笉鍑烘潵public_dd())
            var self = this;
            this.i = 1;
            this.aa = function(){
                this.i ++;
                alert(this.i);
            }
            var private_bb = function(){
                alert(self.i);
                //self.public_dd();//閿欒 self鏃犳硶浠庡閮ㄨ闂?鍚屾椂self涔熸棤娉曡杩欎釜瀵硅薄鐨勫叕鍏辨柟娉曟墍璁块棶
                //aa();//閿欒  绉佹湁鏂规硶瑕侀€氳繃self璋冪敤
                public_dd();//鍙互鐩存帴璋冪敤 涓嶈兘鐢╯elf.public_dd();
                self.aa();
            }
            this.cc = function(){
                private_bb();//绉佹湁鍑芥暟
            }
            
            //鍙互鐩存帴璋冪敤
            //  瀵硅薄鐨勫叕鍏辨柟娉?
            function public_dd()
            {
                self.aa();
                alert("dd");
            }
        }
        var o = new Class1();//璋冪敤Class1鏋勯€犲嚱鏁颁笉杩愯++(鍒濆鍖栨病鏈夎皟鐢ㄤ笉杩愯)
        o.cc();//杩愯++
        document.write(o.i);//return 2




浣跨敤var澹版槑鐨勫彉閲忥紝姣忎釜鍙橀噺鐨勪綔鐢ㄥ煙閮芥槸灞€閮ㄧ殑锛岃繖灏辨剰鍛崇潃瀹冧滑鍙兘鍦ㄥ畠浠墍瀹氫箟鐨勫嚱鏁板唴閮ㄨ闂€傚鏋滀笉浣跨敤var鐨勮瘽锛屽彉閲忕殑浣滅敤鍩熷氨鏄叏灞€鐨勶紝涔熷氨鏄彉閲忓彲浠ヨWeb椤甸潰涓换浣曞湴鏂圭殑鎵€鏈塉avaScript浠g爜锛堟垨鑰呭湪鏈〉闈㈡墍鍖呭惈鐨勪换浣曞閮↗S搴撲腑锛夎闂€?
濡傛灉浣犳湁鐩稿悓鍚嶅瓧鐨勫叏灞€鍙橀噺鍜屽眬閮ㄥ彉閲忕殑璇濓紝璁惧畾鍙橀噺鐨勪綔鐢ㄥ煙寰堥噸瑕併€備粠寮€濮嬪氨鍏绘垚鑹ソ鐨凧avaScript缂栫▼涔犳儻銆傝繖绉嶄範鎯箣涓€灏辨槸锛氭樉寮忓湴瀹氫箟鍙橀噺鐨勪綔鐢ㄥ煙銆?
涓嬮潰鏄竴浜涜€冭檻浣滅敤鍩熸椂鐨勮鍒欙細
濡傛灉鍑芥暟涓殑鍙橀噺鍦ㄥ0鏄庢椂浣跨敤浜嗗叧閿瓧var锛屽畠灏卞彧鑳藉湪姝ゅ嚱鏁扮殑灞€閮ㄤ娇鐢ㄣ€?
濡傛灉鍑芥暟涓殑鍙橀噺鍦ㄥ0鏄庢椂娌℃湁浣跨敤鍏抽敭瀛梫ar锛岃€屽張瀛樺湪涓€涓悓鍚嶇殑鍏ㄥ眬鍙橀噺锛屽畠灏变細琚綋浣滈偅涓叏灞€鍙橀噺銆?
濡傛灉鍙橀噺鍦ㄥ眬閮ㄤ腑澹版槑骞朵娇鐢ㄤ簡鍏抽敭瀛梫ar锛屼絾瀹冨張娌℃湁琚垵濮嬪寲锛堟瘮濡傝祴鍊硷級锛屽畠鍙互璁块棶浣嗘病鏈夎瀹氫箟銆?
濡傛灉鍙橀噺鍦ㄥ眬閮ㄤ腑澹版槑锛屼絾娌℃湁浣跨敤鍏抽敭瀛梫ar锛屾垨鏄惧紡鍦板0鏄庝负鍏ㄥ眬鍙橀噺锛屼絾瀹冨張娌℃湁琚垵濮嬪寲锛屽畠鍦ㄥ叏灞€涓婂彲浠ヨ闂絾娌℃湁琚畾涔夈€?
閫氳繃鍦ㄥ嚱鏁颁腑浣跨敤var锛屼綘鍙互闃叉浣跨敤鍚屽悕鐨勫叏灞€鍙橀噺鍜屽眬閮ㄥ彉閲忔墍閫犳垚鐨勯棶棰樸€傝繖鍦ㄤ娇鐢ㄥ閮↗avaScript搴撶殑鏃跺€欏挨涓哄叧閿€?






getCatalogTree:function(CId,strId){
 ...
},
Oper:function(catalogId){
 this.getCatalogTree(0,"tree");//鍦ㄥ璋冪敤 鍒殑鏂规硶this
 this.option.onSuccess = function(transport){
            if(self.ChkMng(transport.responseText))
     {
          self.getCatalogTree(0,"tree");//鍦ㄩ噷闈?璋冪敤 鍒殑鏂规硶self
     } 
        };
}