什么"!" 运算符在javascript中与非布尔变量一起使用时的意思?

dez*_*cus 13 javascript operators

在阅读javascript代码时,我一直看到! operator used for non boolean variables. Here is an example of code not used in.

/**
 * loads a resource from a url
 * @param {string} url the url of the resource to load
 * @param {string} relativeTo the url to load relative to
 * @param {function} callback thefunction to call once the file is loaded
 * @private
 */
 GLGE.Wavefront.prototype.loadFile=function(url,relativeTo,callback){
    if(this.relativeTo && !relativeTo) relativeTo=this.relativeTo; //<-- used on a string?
    else this.relativeTo=url;
    if(!callback) callback=this.loaded;    //<-- used on a function?
    var req = new XMLHttpRequest();
    if(req) {
               // request handling code                
            }
        };
        req.open("GET", url, true);
        req.send("");
    }   
}
Run Code Online (Sandbox Code Playgroud)

In this library I've seen many uses of this operator in this manner.

Can someone explain how/if the 'not' function of a string, object or function can be determined when it isn't one half of a Boolean set like the set; true运算符用于非布尔变量.以下是未使用的代码示例.

/**
 * loads a resource from a url
 * @param {string} url the url of the resource to load
 * @param {string} relativeTo the url to load relative to
 * @param {function} callback thefunction to call once the file is loaded
 * @private
 */
 GLGE.Wavefront.prototype.loadFile=function(url,relativeTo,callback){
    if(this.relativeTo && !relativeTo) relativeTo=this.relativeTo; //<-- used on a string?
    else this.relativeTo=url;
    if(!callback) callback=this.loaded;    //<-- used on a function?
    var req = new XMLHttpRequest();
    if(req) {
               // request handling code                
            }
        };
        req.open("GET", url, true);
        req.send("");
    }   
}
Run Code Online (Sandbox Code Playgroud)

在这个库中,我以这种方式看到了这种运算符的许多用途.

有人可以解释当字符串,对象或函数的"非"函数不是像集合那样的布尔集的一半时,是否可以确定它是什么?false! operator used for non boolean variables. Here is an example of code not used in.

/**
 * loads a resource from a url
 * @param {string} url the url of the resource to load
 * @param {string} relativeTo the url to load relative to
 * @param {function} callback thefunction to call once the file is loaded
 * @private
 */
 GLGE.Wavefront.prototype.loadFile=function(url,relativeTo,callback){
    if(this.relativeTo && !relativeTo) relativeTo=this.relativeTo; //<-- used on a string?
    else this.relativeTo=url;
    if(!callback) callback=this.loaded;    //<-- used on a function?
    var req = new XMLHttpRequest();
    if(req) {
               // request handling code                
            }
        };
        req.open("GET", url, true);
        req.send("");
    }   
}
Run Code Online (Sandbox Code Playgroud)

In this library I've seen many uses of this operator in this manner.

Can someone explain how/if the 'not' function of a string, object or function can be determined when it isn't one half of a Boolean set like the set; true

Gar*_*ies 15

任何虚假价值都将满足if(!insert_variable_here)条件,包括:

  • false
  • null
  • undefined
  • 空字符串 ''
  • 数字 0
  • NaN

如果callbackreturn评估任何这些值,则满足条件.

即便如此null != false,以下内容会给您提醒:

x = null;
if(!x) {
    alert('"!null" does evaluate to true');
}
Run Code Online (Sandbox Code Playgroud)

无论是否null != false对您或其他任何人有意义,重点是JavaScript null中的值是一个假值,因此是一个满足上面第一部分代码条件的值.这似乎是你问过的问题 -而不是,是否null应该== false.


mae*_*ics 9

在JavaScript中,一元否定运算符(!)将基于语言的(有点令人困惑的)规则将其操作数转换为布尔值(例如,ECMA-262第5版). 这篇关于JavaScript语法的文章展示了类型转换如何发生的一些示例.

基本上,这是一种测试非"真实性"的简单方法; 看似假值(例如false,null,0,NaN,空字符串等)将被转换为false逻辑上否定之前,反之亦然.您可以使用布尔构造函数显式测试"真实性":

Boolean(null); // => false
Boolean(NaN); // => false
Boolean(""); // => false
Boolean(0); // => false
Boolean(1); // = >true
Boolean("123"); // => true
Boolean(new Object()); // => true
Boolean(function(){}); // => true
Run Code Online (Sandbox Code Playgroud)