面试官:JavaScript中“x !== x”可以返回True吗?

开发 前端
在面试的过程中,你有被问一些奇怪面试题的经历吗?这些面试题与常规问题不同:这些面试问题看起来很简单,但却考验你对 JavaScript 的透彻理解,今天我将它们整理出来,看看你是否都能回答出来。

在面试的过程中,你有被问一些奇怪面试题的经历吗?这些面试题与常规问题不同:这些面试问题看起来很简单,但却考验你对 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

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')
}

也许你知道“对象”,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

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

这个问题可能并不容易,但只要你了解 JavaScript,你就会知道“Number.MAX_SAFE_INTEGER 常量代表 JavaScript 中的最大安全整数 (²⁵³ — 1)”。


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')
}

4.“x > x”可以为true吗?

我不想再看书了,这是什么垃圾问题?

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')
}

哇,太神奇了!

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” 为 true?

答案是文档。All 一个 HTMLAllCollection,包含文档中的每个元素(来自 MDN)。

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)

最后

以上就是我跟你分享的全部内容,希望对你有用,最后,感谢你的阅读,并期待你的关注,阅读更多文章内容。

责任编辑:华轩 来源: web前端开发
相关推荐

2022-05-10 12:02:44

面试JavaScriptJS

2022-12-22 14:32:37

JavaScript编程语言

2023-08-11 17:13:39

JavaScrip

2022-07-15 08:22:42

对象符串键Symbol

2023-07-11 16:01:47

共享数据开发

2023-06-20 12:02:39

WhileFor(;;)

2021-09-01 07:21:41

面试官开发读写锁

2022-08-02 06:31:32

Java并发工具类

2022-07-26 08:40:42

Java并发工具类

2022-10-08 08:00:00

JavaScript数组开发

2022-07-11 10:47:46

容器JAVA

2022-06-30 08:14:05

Java阻塞队列

2022-05-23 08:43:02

BigIntJavaScript内置对象

2020-05-11 14:55:44

CSS鼠标前端

2015-08-13 10:29:12

面试面试官

2021-04-12 21:34:29

Redis故障数据

2021-09-08 08:06:57

Redis原子性数据类型

2024-01-10 15:27:58

SessionCookieWeb 应用

2020-03-06 15:36:01

Redis内存宕机

2021-03-16 07:13:07

Java对象存储
点赞
收藏

51CTO技术栈公众号