一个对象怎么能等于一个空字符串? [英] How can an object be equal to an empty string?

查看:133
本文介绍了一个对象怎么能等于一个空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我注意到这个有趣的细节时,我正在玩Google Apps Script API,如以下代码所述

 函数testEmptyRange(){
var range = SpreadsheetApp.getActive()。getSheetByName(Sheet1)。getDataRange();
var values = range.getValues();
Logger.log(values.length ++ values [0] .length);
Logger.log((values [0] [0] ==)++ typeof values [0] [0]);
Logger.log((values [0] ==)++ typeof values [0]);
Logger.log((values ==)++ typeof values);

$ / code>

如果 Sheet1 没有任何内容,那么 values 1乘1的对象二维数组,实际上是包含空字符串的单个单元格。真正让我感到惊讶的是, 值[0] code>,它们是类型 object ,也等于空字符串。由上述函数产生的日志是

  [17-03-05 00:43:46:807 EST] 1 1 
[17-03-05 00:43:46:807 EST] true string
[17-03-05 00:43:46:808 EST] true object
[17-03- 05 00:43:46:808 EST] true object

我想解释一下为什么这个是这样的。一个对象如何能等于空字符串?我知道运算符 == 不同于运算符 === ,如果我要替换前者与后者,则日志显示值[0]的 false ,我认为这更符合预期的行为。但是这并不能解释为什么一个对象在第一种情况下可以等于一个空字符串,我想知道这里发生了什么。 解决方案

问题是,正如你所提到的那样, == 并不代表最严格的比较。 == 在其经常失败的尝试中使用它的参数的类型强制来对我们的程序员更有帮助。



当在比较两个值之前, == 的一个参数是string类型的,另一个是 always 转换为字符串。因此,对于一个对象,JavaScript引擎最终会做这样的事情:


  1. object ==


  2. String(object)===


  3. object.toString()===


因为一个对象可以有任何 toString 方法给你,所以这个比较的输出可能是 true



编辑anied 上面的评论,并重新读您的示例代码,它打我 values [0] 实际上是数组,这是对象类型的特例。它们有一个 toString 方法,它用逗号将所有数组元素连接在一起,这意味着一个空数组 [] 将会是字符串化为



示例:



  var object = {toString:function(){return''}} console.log(typeof object)// => 'object'console.log(object ==)// => truevar array = [] console.log(typeof array)// => 'object'console.log(array ==)// => truevar matrix = [['']] console.log(typeof matrix)// => 'object'console.log(matrix ==)// => true  


I was playing around with the Google Apps Script API when I noticed this interesting detail, as outlined in the following code

function testEmptyRange() {
  var range = SpreadsheetApp.getActive().getSheetByName("Sheet1").getDataRange();
  var values = range.getValues();
  Logger.log(values.length + " " + values[0].length);
  Logger.log((values[0][0] == "") + " " + typeof values[0][0]);
  Logger.log((values[0] == "") + " " + typeof values[0]);
  Logger.log((values == "") + " " + typeof values);
}

If Sheet1 has no content whatsoever, then values is a 1-by-1 2D array of Objects, effectively a single cell containing an empty string. What really surprised me is the fact that both values and values[0], which are type object, are also equal to the empty string. The log produced by the above function is

[17-03-05 00:43:46:807 EST] 1 1
[17-03-05 00:43:46:807 EST] true string
[17-03-05 00:43:46:808 EST] true object
[17-03-05 00:43:46:808 EST] true object

I would like an explanation as to why this is the case. How can an object be equal to the empty string? I realize that the operator == is different from the operator ===, and that if I were to replace the former with the latter, then the log shows false for both values and values[0], which is more along the lines of expected behavior, I suppose. But that doesn't explain why or how an object can equal an empty string in the first case, and I would like to understand what's going on here.

解决方案

The problem is, as you mentioned, that == does not represent the strictest kind of comparison. == uses type coercion of its arguments in an often failed attempt to be more helpful to us programmers.

When one argument of == is of type string, the other is always casted to a string before the two values are compared. Thus, for an object, the JavaScript engine ends up doing something like this:

  1. object == ""

  2. String(object) === ""

  3. object.toString() === ""

Since an object can have any toString method you give it, the output of this comparison could potentially be true.

Edit: Looking at anied's comment above and re-reading your example code, it hit me that values and values[0] are actually arrays, which are a special case of the object type. They have a toString method that joins together all array elements with commas, which means that an empty array [] will be stringified into ""

Examples:

var object = {
  toString: function () { return '' }
}

console.log(typeof object) //=> 'object'
console.log(object == "") //=> true


var array = []

console.log(typeof array) //=> 'object'
console.log(array == "") //=> true


var matrix = [
  ['']
]

console.log(typeof matrix) //=> 'object'
console.log(matrix == "") //=> true

这篇关于一个对象怎么能等于一个空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆