2013-05-06 25 views
4

我有一些代碼,當變量可以是undefinednull或正常值。代碼需要做同樣的事情,無論是否變量是undefinednull。有沒有說使用變量!= null而不是變量可以接受!== undefined && variable!== null?

for (var cur = this.buckets[i]; cur != null; cur = cur.next) { 

,而不是

for (var cur = this.buckets[i]; cur !== undefined && cur !== null; cur = cur.next) { 

完整程序的危險之下(這行是HashTable.prototype.walk)。

var hash_seed = Math.floor(Math.random() * 256); 

function jenkins_hash(key, interval_size) { 
    var hash = hash_seed; 
    for (var i=0; i<key.length; ++i) { 
     hash += key.charCodeAt(i); 
     hash += (hash << 10); 
     hash ^= (hash >> 6); 
    } 
    hash += (hash << 3); 
    hash ^= (hash >> 11); 
    hash += (hash << 15); 
    // make unsigned and modulo interval_size 
    return (hash >>> 0) % interval_size; 
} 

//constructor, takes the number of buckets to create 
function HashTable(size) { 
    this.buckets = new Array(size); 
} 

//private method, ignore 
HashTable.prototype._position = function(key) { 
    var index = jenkins_hash(key, this.buckets.length); 
    var cur = this.buckets[index]; 

    if (cur === undefined) { 
      return { i: index, cur: null, prev: null }; 
    } 

    var prev = cur; 
    for (; cur !== null; cur = cur.next) { 
     if (cur.key == key) { 
      return { i: index, cur: cur, prev: prev }; 
     } 
     prev = cur; 
    } 

    return { i: index, cur: cur, prev: prev }; 
}; 

// associate a value with a key in the hash 
HashTable.prototype.store = function(key, value) { 
    var r = this._position(key); 
    if (r.prev === null) { 
      this.buckets[r.i] = { 
       key: key, value: value, next: null 
      }; 
      return; 
    } 

    if (r.cur !== null) { 
     r.cur.value = value; 
     return; 
    } 

    r.prev.next = { 
     key: key, value: value, next: null 
    }; 
    return; 
}; 

// fetches the value associated with the key 
// returns undefined if the key is not in the hash 
HashTable.prototype.fetch = function(key) { 
    var r = this._position(key); 
    if (r.cur === null) { 
     return undefined; 
    } 

    return r.cur.value; 
}; 

// returns true if the key is in the hash 
// returns false if the key isn't in the hash 
HashTable.prototype.exists = function(key) { 
    var r = this._position(key); 
    return r.cur !== null; 
}; 

// removes a key from the hash 
HashTable.prototype.delete = function(key) { 
    var r = this._position(key); 
    if (r.cur === null) { 
     return; 
    } 
    if (r.cur === r.prev) { 
     this.buckets[r.i] = r.cur.next; 
    } 
    r.prev.next = r.cur.next; 
}; 

// removes all keys from the hash 
HashTable.prototype.clear = function() { 
    var length = this.buckets.length; 
    this.buckets = new Array(length); 
}; 

// run a funciton for every key/value pair in the hash 
// function signature should be function(k, v) {} 
// WARNING: adding keys to the hash while this method is 
// running may lead to unexpected results 
HashTable.prototype.walk = function(func) { 
    for (var i = 0; i < this.buckets.length; i++) { 
     for (var cur = this.buckets[i]; cur != null; cur = cur.next) { 
      func(cur.key, cur.value); 
     } 
    } 
}; 

// returns all of the keys in the hash 
HashTable.prototype.keys = function() { 
    var keys = []; 
    this.walk(function(k,v) { keys.push(k) }); 
    return keys; 
}; 

// run a function for every key/value pair in the hash 
// function signature should be function(k, v) {} 
// WARNING: only keys in the hash when the method is called 
// will be visited; however, changes to their values will be 
// reflected 
HashTable.prototype.safer_walk = function(func) { 
    var keys = this.keys(); 
    for (var i = 0; i < keys.length; i++) { 
     func(keys[i], this.fetch(keys[i])); 
    } 
} 

var h = new HashTable(101); 

h.store("abc", 5); 
h.store("def", 6); 
h.walk(function(k, v) { console.log(k + " => " + v) }); 
+0

只需發表一條評論,即使用'!=我的想法是。儘管如此,我不確定這是否是真正的答案。 – 2013-05-06 14:22:44

+0

@ Qantas94Heavy如果非要加上一個註釋,使之清楚,其他的程序員,那麼我也許應該只是將其更改爲後者。 – 2013-05-06 14:26:16

+0

這並不是說,目前還不清楚(如尖說,該算法是在規範) - 那就是他們可能會認爲你的目的只是比較空,沒有undefined和null。 – 2013-05-06 14:29:02

回答

5

!=的語義是明確定義的。如果你比較null,則:

  • 如果 「小人」 是undefinednull,那麼結果是false
  • 如果 「小人」 是什麼東西,那麼結果是true

「抽象」相等比較(如嚴格比較)首先檢查每個操作數的類型(注意,與typeof返回的值不同)。的類型的null是空類型和undefined類型是未定義的類型。抽象比較算法明確地認爲undefinednull是相等的。

因此,對nullundefined進行自己的顯式檢查確實沒有意義。 (當然,如果你需要邏輯,不同於內置在語言中的抽象比較)。

+0

男孩做我自己混淆試圖總結''===這個:-) – Pointy 2013-05-06 14:22:50

+1

如果我讀你寫正確的,那麼有沒有危險:兩個語句功能相同(我可以忽略警告我的編輯正在投擲)。 – 2013-05-06 14:24:11

+0

@ Chas.Owens是的,我只是添加了一些東西。 [規範在這裏。](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) – Pointy 2013-05-06 14:26:22