我知道的JavaScript
	1. JavaScript闭包
	代码:
	view plain
	<pre name="code" class="javascript">(function(){ 
	var validator_elements_blur_selector   = ‘…’; 
	    var validator_elements_change_selector = ‘…’; 
	var validator_elements_selector = ‘…’; 
	//some other codes… 
	})(); 
	解释:以上代码中的三个变量的作用域就在整个( )之间,外部无法改变这三个变量的值。这样就可以防止第三方代码对自己本身逻辑的侵入。这也是现在很多Js框架常用的自我保护的方法。代码最后的( )是对这个function 执行操作。前面的( )是对function 进行对象化的包装。这样这段逻辑就可以在代码所在的上下文立即执行了。
	2. Javascript 数据结构之– Hashtable
	代码:
	view plain
	<pre name="code" class="javascript">function Hashtable() { 
	    this._hashValue= new Object(); 
	    this._iCount= 0; 
	} 
	Hashtable.prototype.add = function(strKey, value) { 
	    if(typeof (strKey) == "string"){ 
	        this._hashValue[strKey]= typeof (value) != "undefined"? value : null; 
	        this._iCount++; 
	        returntrue; 
	    } 
	    else 
	        throw"hash key not allow null!"; 
	} 
	Hashtable.prototype.get = function (key) { 
	if (typeof (key)== "string" && this._hashValue[key] != typeof('undefined')) { 
	        returnthis._hashValue[key]; 
	    } 
	    if(typeof (key) == "number") 
	        returnthis._getCellByIndex(key); 
	    else 
	        throw"hash value not allow null!"; 
	  
	    returnnull; 
	} 
	Hashtable.prototype.contain = function(key) { 
	    returnthis.get(key) != null; 
	} 
	Hashtable.prototype.findKey = function(iIndex) { 
	    if(typeof (iIndex) == "number") 
	        returnthis._getCellByIndex(iIndex, false); 
	    else 
	        throw"find key parameter must be a number!"; 
	} 
	Hashtable.prototype.count = function () { 
	    returnthis._iCount; 
	} 
	Hashtable.prototype._getCellByIndex = function(iIndex, bIsGetValue) { 
	    vari = 0; 
	    if(bIsGetValue == null) bIsGetValue = true; 
	    for(var key in this._hashValue) { 
	        if(i == iIndex) { 
	            returnbIsGetValue ? this._hashValue[key] : key; 
	        } 
	        i++; 
	    } 
	    returnnull; 
	} 
	Hashtable.prototype.remove = function(key) { 
	    for(var strKey in this._hashValue) { 
	        if(key == strKey) { 
	            deletethis._hashValue[key]; 
	            this._iCount--; 
	 &
	
相关新闻>>
- 发表评论
- 
				
- 最新评论 进入详细评论页>>





