自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 前端
在面試的過程中,你有被問一些奇怪面試題的經(jīng)歷嗎?這些面試題與常規(guī)問題不同:這些面試問題看起來很簡單,但卻考驗(yàn)?zāi)銓?JavaScript 的透徹理解,今天我將它們整理出來,看看你是否都能回答出來。

在面試的過程中,你有被問一些奇怪面試題的經(jīng)歷嗎?這些面試題與常規(guī)問題不同:這些面試問題看起來很簡單,但卻考驗(yàn)?zāi)銓?JavaScript 的透徹理解,今天我將它們整理出來,看看你是否都能回答出來。

1.“x !== x”可以返回true嗎?

要輸出“hello fatfish”,“x”的值應(yīng)該是多少?

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 嗎?

好吧,當(dāng)我們過濾掉“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 中的最大安全整數(shù) (2?3 — 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')
}

我不得不承認(rèn) 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)

最后

以上就是我跟你分享的全部內(nèi)容,希望對你有用,最后,感謝你的閱讀,并期待你的關(guān)注,閱讀更多文章內(nèi)容。

責(zé)任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

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

2024-07-22 14:09:22

@AsyncJava

2024-09-18 09:02:14

單核服務(wù)器線程切換

2024-09-11 08:38:02

JavaScriptC#語言編程

2023-07-11 16:01:47

共享數(shù)據(jù)開發(fā)

2023-06-20 12:02:39

WhileFor(;;)

2022-10-08 08:00:00

JavaScript數(shù)組開發(fā)

2021-09-01 07:21:41

面試官開發(fā)讀寫鎖

2022-07-26 08:40:42

Java并發(fā)工具類

2022-08-02 06:31:32

Java并發(fā)工具類

2024-05-28 10:14:31

JavaScrip模板引擎

2022-06-30 08:14:05

Java阻塞隊(duì)列

2022-05-23 08:43:02

BigIntJavaScript內(nèi)置對象

2022-07-11 10:47:46

容器JAVA

2015-08-13 10:29:12

面試面試官

2020-03-06 15:36:01

Redis內(nèi)存宕機(jī)

2024-10-16 15:01:20

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號