当前位置:  开发笔记 > 编程语言 > 正文

是否可以使用变量!= null而不是变量!== undefined && variable!== null?

如何解决《是否可以使用变量!=null而不是变量!==undefined&&variable!==null?》经验,为你挑选了1个好方法。

我有一些代码,其中一个变量可能是undefined,null或正常值.该代码需要做同样的事情,不管变量是否是undefinednull.说话有危险吗?

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> 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) });

Pointy.. 5

语义!=很明确.如果你比较null,那么:

如果"cur"是undefinednull,那么结果是false

如果"cur"是其他任何东西,那么结果是 true

"抽象"相等比较(如严格比较)首先检查每个操作数的类型(注意,它与typeof返回的不同!).类型为nullNull类型,类型undefined为Undefined类型.抽象比较算法明确地考虑undefined并且null是相等的.

因此,真的没有一点做既为自己明确的检查nullundefined.(如果您需要的逻辑与语言中内置的抽象比较不同,您可能需要单独检查.)



1> Pointy..:

语义!=很明确.如果你比较null,那么:

如果"cur"是undefinednull,那么结果是false

如果"cur"是其他任何东西,那么结果是 true

"抽象"相等比较(如严格比较)首先检查每个操作数的类型(注意,它与typeof返回的不同!).类型为nullNull类型,类型undefined为Undefined类型.抽象比较算法明确地考虑undefined并且null是相等的.

因此,真的没有一点做既为自己明确的检查nullundefined.(如果您需要的逻辑与语言中内置的抽象比较不同,您可能需要单独检查.)

推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有