我知道的JavaScript

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 17:31 点击:

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--; 
 &

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Gb2312转utf-8编码的方法(vbs+js)
      • 如何使用Ajax技术开发Web应用程序(1)
      • js跳转路径问题
      • JavaScript模仿桌面窗口
      • 用js检测两个线段是否相交
      • 运用JavaScript构建你的第一个Metro式应用程序(on Windows
      • 我知道的JavaScript -- 设计模式(桥接)应用之 – 验证器
      • 我是如何去了解jquery的(六),案例之幻灯片轮换
      • Jquery封装幻灯片效果
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1