与 `{}` 和真实性比较

comparing with `{}` and truthyness

我有一个 API 用 {} 调用我需要替换的参数。 但是遇到了麻烦......你能猜出下面会发生什么吗?

const log = console.log

const a = {}

log('a is ', a)

// cannot compare with either == or ===
if (a == {}) {
  log('a is {}')
} else {
  log('a is NOT {}')
}

// a is truthy too
const b = (a || 'not a')

console.log('b is ', b)

鉴于它既真实又无法比较,我想知道如何替换它?

我也很想知道为什么,幕后进行的是什么对象比较。

我的理解是:

但我想我需要对对象进行实际比较?我习惯用 toBe()toEqualtoDeepEqual 工作的单元测试框架来做这件事。

WAT!

你可以试试这个

const a = {}

log=console.log
log('a is ', a)

// cannot compare with either == or ===
if (typeof a == "object"&& Object.keys(a).length==0) {// the && part to check if the object is empty
  log('a is {}')
} else {
  log('a is NOT {}')
}

如果您需要做的只是检测某物是否为空对象,那么这样的方法可能会起作用:

const a = {};
const b = { hello: 'world' };
const c = 'test';

function isEmptyObject(arg) {
  return typeof arg === 'object' && Object.keys(arg).length === 0;
}

console.log(`a is ${isEmptyObject(a)}`);
console.log(`b is ${isEmptyObject(b)}`);
console.log(`c is ${isEmptyObject(c)}`);

对象始终具有真布尔值(对象存在?然后为真)。像字符串和数字这样的基元是通过它们的值来比较的,而像数组、日期和普通对象这样的对象是通过它们的引用来比较的。通过引用进行比较基本上是检查给定的对象是否引用内存中的相同位置。您可以通过 JSON.stringify.

检查对象是否相等

const a = {}

if (JSON.stringify(a) === JSON.stringify({})) {
  log('a is {}')
} else {
  log('a is NOT {}')
}

console.log(Boolean({}));   //Object exist?: True

const b = (a || 'not a')
console.log('b is ', b)

也许这会澄清事情:

let a = {}; // Object Literal
let b = {}; // Object Literal
let c = a;  

// both a and b above are separate objects

console.log(b == a)   // false
console.log(c == a)   // true
console.log({} == {}) // false, as both are different Objects

// if you want to test if two things are the same type you could do
console.log(({}).toString() == ({}).toString()) // true
console.log(a.toString() == b.toString()) // true

// the reason why those work is, because now we are comparing strings... not the objects
console.log(({}).toString()) // "[object Object]"

// to simply check that the Object is "true", you could.
console.log(!!{});

// Specifying your own false, if empty comparisons "as values"...

console.log(!!Object.keys({}).length) // false
// or, even
console.log( !("{}" == JSON.stringify({}) ) ) // false

如果感兴趣的对象有任何可能是一个函数或包含任何非 json 安全值,我会警告不要使用 JSON.stringify,因为在这种情况下你会得到一个错误你比较中的功能或误报


JavaScript 平等

=== 是严格相等比较 ("identity")

== 是抽象相等比较(“松散相等”)

"==="很简单,被比较的对象是不是同一个对象“实例”

“==”有点复杂,因为强制起作用,所以它更像是“它们能一样吗”

查看此 article,其中介绍了正在发生的事情的机制...

Loose Equals vs. Strict Equals

Loose equals is the == operator, and strict equals is the === operator. Both operators are used for comparing two values for "equality," but the "loose" vs. "strict" indicates a very important difference in behavior between the two, specifically in how they decide "equality."

A very common misconception about these two operators is: "== checks values for equality and === checks both values and types for equality." While that sounds nice and reasonable, it's inaccurate. Countless well-respected JavaScript books and blogs have said exactly that, but unfortunately they're all wrong.

The correct description is: "== allows coercion in the equality comparison and === disallows coercion.