为什么 JavaScript 有时会将 null 转换为空字符串,有时会转换为“null”字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26971097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:45:49  来源:igfitidea点击:

Why does JavaScript sometimes convert null to empty string and sometimes to "null" string?

javascriptstringnull

提问by Anatolii Humennyi

I'm studying JavaScript and I'm confused about the following:

我正在学习 JavaScript,但对以下内容感到困惑:

document.getElementById("demo").innerHTML = typeof (document.getElementById("demo").innerHTML);

It assigns "string" to an HTML element. OK. So then I suppose when I will try to assign some value of different type (not string) it will be converted to string first:

它将“字符串”分配给 HTML 元素。行。那么我想当我尝试分配一些不同类型(不是字符串)的值时,它将首先转换为字符串:

document.getElementById("demo").innerHTML = null; //null will be converted to string first

like this:

像这样:

document.getElementById("demo").innerHTML = String(null);

HTML element gets en empty value in the first example. But the second example returns "string". Why? This tutorial http://www.w3schools.com/js/js_type_conversion.aspsays that String(null) returns "null", but not empty string. As I see it's true only for the second example.

HTML 元素在第一个示例中获得空值。但第二个示例返回“字符串”。为什么?本教程http://www.w3schools.com/js/js_type_conversion.asp说 String(null) 返回“null”,但不是空字符串。正如我所见,它仅适用于第二个示例。

How to understand this confusing conversion behavior of JavaScript? Why doesn't the first example return "string"?

如何理解 JavaScript 这种令人困惑的转换行为?为什么第一个示例不返回“字符串”?

Edited:

编辑:

Now partially I understood it. JS uses toString()but not String(). And toString()returns empty string for null. So now my question is: Why toString()and String()produce different values?

现在我部分理解了。JS 使用toString()但不是String(). 并toString()为 null 返回空字符串。所以,现在我的问题是:为什么toString()String()产生不同的价值观?

Edited 2:

编辑 2:

Examples:

例子:

document.getElementById("demo").innerHTML = String(null); //returns an empty string 
document.getElementById("demo").innerHTML = null.toString(); //returns "null" string

采纳答案by Ja?ck

I don't think there's a real convention here; Element.innerHTMLis a property that tests the given value to determine what to do. In Safari it behaves like this:

我认为这里没有真正的公约;Element.innerHTML是测试给定值以确定要执行的操作的属性。在 Safari 中,它的行为是这样的:

if (value === null || value === '') {
    // remove all contents
} else {
    // parse string representation of value into the elements contents
}

So both ""(empty string) and nullare considered the same and the assignment will just remove all contents; I couldn't find conclusive evidence that would suggest other browsers work this way, but it seems very likely that it should be considered an implementation detail that you shouldn't rely upon (see update).

所以""(空字符串) 和null都被认为是相同的,并且赋值只会删除所有内容;我找不到确凿的证据表明其他浏览器以这种方式工作,但它似乎很可能应该被视为您不应该依赖的实现细节(请参阅更新)。

That said, the documented way of clearing an element is by assigning the empty string to this property.

也就是说,记录的清除元素的方法是将空字符串分配给此属性。

Update

更新

I've found this (inconclusive) email threadabout the subject, highlighting that this behaviour is not standardised:

我发现了这个关于这个主题的(不确定的)电子邮件线程,强调这种行为不是标准化的:

For .innerHTML = null Opera and Internet Explorer act as if the literal string "null" was used. Firefox acts as if "" was used.

对于 .innerHTML = null Opera 和 Internet Explorer 就像使用了文字字符串“null”一样。Firefox 的行为就像使用了“”。

回答by laruiss

You wrote:

你写了:

document.getElementById("demo").innerHTML = String(null); //returns an empty string 
document.getElementById("demo").innerHTML = null.toString(); //returns "null" string
document.getElementById("demo").innerHTML = String(null); //returns an empty string 
document.getElementById("demo").innerHTML = null.toString(); //returns "null" string

But both assertions are false, I am afraid.

但恐怕这两种说法都是错误的。

String(null)never returns an empty string, rather a primitive of type stringwhose value is "null".

String(null)从不返回空字符串,而是返回string值为“null”的类型原语。

BTW, The form String(null)should neverbe used.

顺便说一句,该表格String(null)应该永远不会被使用

new String(null), on the other hand, returns an object, instance of String(note the uppercase first letter) whose primitive value ([[PrimitiveValue]]internal property) is "null".

new String(null),另一方面,返回一个对象,实例String(注意大写首字母),其原始值([[PrimitiveValue]]内部属性)是"null"

null.toString()raises an error in every JS engine I know : event though nullmight be considered an object (due to a historical bug), it has no property, therefore no 'method' toString()(I quote the 'method' because there are no method in JS, really).

null.toString()在我知道的每个 JS 引擎中都会引发错误:事件虽然null可能被认为是一个对象(由于历史错误),但它没有属性,因此没有“方法” toString()(我引用“方法”是因为 JS 中没有方法,真的)。

Anyway, to be consistent, you could use this :

无论如何,为了保持一致,你可以使用这个:

document.getElementById("demo").innerHTML = whateverVariable || '';

Should whateverVariablebe falsy (null, undefined, 0, -0, '', NaNor false), empty string ''will be assigned to document.getElementById("demo").innerHTML.

应该whateverVariable是假的(null, undefined, 0, -0, '', NaNor false),空字符串''将被分配给document.getElementById("demo").innerHTML