“x !== x” 在 JS 中会返回 True 吗?

048d7b7400ba5d0a64644f44893466ff.png

英文 | https://javascript.plainenglish.io/interviewer-can-x-x-return-true-in-javascript-7e1d1fa7b5cd

翻译 | 杨小爱

前言

我们在面试的时候,会被问到一些奇怪的面试问题。它们与常规问题不同:这些面试问题看起来很简单,但它们会测试你对 JavaScript 的理解是否透彻。

现在,我们一起来看看,自己能答对几个?

1、“x !== x”可以返回true吗?

输出“hello fatfish”的“x”值应该是多少?

const x = ? // Please fill in the value of "x?
if (x !== x) {
  console.log('hello fatfish')
}

太奇妙了,是否存在不等于自身的值?但是,JavaScript 中有一个值 NaN,它不等于任何值,甚至不等于自身。

const x = NaN // Please fill in the value of "x?
if (x !== x) {
  console.log('hello fatfish')
}
console.log(NaN === NaN) // false
console.log(x !== x) // true
console.log(Number.isNaN(x)) // true

0ac197aa21dfcb383016153b4e44c0a4.png

2、(!isNaN(x) && x !== x) 可以返回true吗?

好的,当我们过滤掉“NaN”时,还有什么值可以让一个值不等于自己呢?

const x = ? // Please fill in the value of "x?
if(!isNaN(x) && x !== x) {
  console.log('hello fatfish')
}

也许你知道“Object.Defineproperty”,它可以帮助我们解决这个问题。

window.x = 0 // Any value is OK
Object.defineProperty(window, 'x', {
  get () {
    return Math.random()
  }
})
console.log(x) // 0.12259077808826002
console.log(x === x) // false
console.log(x !== x) // true

ae6957aee223c9a08ca768637c00f9fb.png

3、如何使“x === x + 1”?

这个问题可能并不容易,但只要你了解 JavaScript,你就会知道“Number.MAX_SAFE_INTEGER 常量代表 JavaScript 中的最大安全整数 (2⁵³ — 1)。”(来自 MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)

const x = ? // Please fill in the value of "x?
if (x === x + 1) {
  console.log('hello fatfish')
}

所以,我们可以为“x”分配任何大于“Number.MAX_SAFE_INTEGER”的值。

const x =  Number.MAX_SAFE_INTEGER + 1// Please fill in the value of "x?
if (x === x + 1) {
  console.log('hello fatfish')
}

f04e3287ed2dd229c0f2199cf810799a.png

4、 “x > x”可以是真的吗?

这是什么垃圾问题?我不想再读了。

const x = ? // Please fill in the value of "x?
if (x > x) {
  console.log('hello fatfish')
}

虽然,看起来不太可能,但是一个值怎么可能大于它自己呢?但是,我们可以使用“Symbol.toPrimitive”功能来完成问题。

const x = { // Please fill in the value of "x?
  value: 1,
  [ Symbol.toPrimitive ] () {
    console.log('x', this.value)
    return --this.value
  }
}


if (x > x) {
  console.log('hello fatfish')
}

c9bf5aed244c66bdd69c6409e1cf5fa1.png

哇,太神奇了!

5、 typeof x === ‘undefined’ && x.length > 0 ?

const x = ? // Please fill in the value of "x?
if(typeof x === 'undefined' && x.length > 0) {
  console.log('hello fatfish')
}

我不得不承认 JavaScript 是一门了不起的语言。除了 undefined 本身,还有什么值可以让 typeof x === undefined” 为真呢?

答案是document. All,包含文档中每个元素的 HTMLAllCollection(来自 MDN:https://developer.mozilla.org/en-US/docs/Web/API/Document/all)。

const x = document.all // Please fill in the value of "x?
if(typeof x === 'undefined' && x.length > 0) {
  console.log('hello fatfish')
}


console.log(x)
console.log(typeof x)
console.log(x === undefined)

最后

感谢你的阅读,如果你觉得我今天分享的内容对你有帮助,请记得点赞我,关注我,并分享给你身边做开发的朋友,也许能够帮助到他。

最后祝你编程愉快!

学习更多技能

请点击下方公众号

42067e8187b2e6d9051f70e8abeaf2e5.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值