日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

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

來源: 責編: 時間:2023-08-14 22:01:18 347觀看
導讀在面試的過程中,你有被問一些奇怪面試題的經歷嗎?這些面試題與常規問題不同:這些面試問題看起來很簡單,但卻考驗你對 JavaScript 的透徹理解,今天我將它們整理出來,看看你是否都能回答出來。1.“x !== x”可以返回true嗎?

在面試的過程中,你有被問一些奇怪面試題的經歷嗎?這些面試題與常規問題不同:這些面試問題看起來很簡單,但卻考驗你對 JavaScript 的透徹理解,今天我將它們整理出來,看看你是否都能回答出來。cmk28資訊網——每日最新資訊28at.com

cmk28資訊網——每日最新資訊28at.com

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

要輸出“hello fatfish”,“x”的值應該是多少?cmk28資訊網——每日最新資訊28at.com

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

太奇妙了。是否存在不等于自身的值?然而,JavaScript 中有一個值 NaN,它不等于任何值,甚至不等于它本身。cmk28資訊網——每日最新資訊28at.com

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

2. (!isNaN(x) && x !== x) 可以返回 true 嗎?

好吧,當我們過濾掉“NaN”時,還有什么其他值可以使一個值不等于它自己呢?cmk28資訊網——每日最新資訊28at.com

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

也許你知道“對象”,Defineproperty”,可以幫助我們解決這個問題。cmk28資訊網——每日最新資訊28at.com

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

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

這個問題可能并不容易,但只要你了解 JavaScript,你就會知道“Number.MAX_SAFE_INTEGER 常量代表 JavaScript 中的最大安全整數 (2?3 — 1)”。cmk28資訊網——每日最新資訊28at.com

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

因此我們可以為“x”分配任何大于“Number.MAX_SAFE_INTEGER”的值。cmk28資訊網——每日最新資訊28at.com

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

我不想再看書了,這是什么垃圾問題?cmk28資訊網——每日最新資訊28at.com

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

雖然看起來不太可能,但一個值怎么可能比它本身更大呢?但是,我們可以使用“Symbol.toPrimitive”功能來完成該問題。cmk28資訊網——每日最新資訊28at.com

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

哇,太神奇了!cmk28資訊網——每日最新資訊28at.com

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?cmk28資訊網——每日最新資訊28at.com

答案是文檔。All 一個 HTMLAllCollection,包含文檔中的每個元素(來自 MDN)。cmk28資訊網——每日最新資訊28at.com

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)

最后

以上就是我跟你分享的全部內容,希望對你有用,最后,感謝你的閱讀,并期待你的關注,閱讀更多文章內容。cmk28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-5726-0.html面試官:JavaScript中“x !== x”可以返回True嗎?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: JavaScript 中有趣的九個常用編碼套路

下一篇: 破解代碼:每個Web開發者都應該知道的七個秘密

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 沁源县| 民勤县| 临邑县| 屏东县| 岑溪市| 罗平县| 淅川县| 射洪县| 云林县| 漳州市| 威宁| 福鼎市| 长阳| 万安县| 高淳县| 四子王旗| 洱源县| 安溪县| 九寨沟县| 东安县| 陆良县| 繁昌县| 靖安县| 兴国县| 德兴市| 邹城市| 英吉沙县| 凤台县| 额敏县| 茂名市| 余庆县| 樟树市| 夏津县| 邛崃市| 东丽区| 松原市| 监利县| 大理市| 清苑县| 临沭县| 新龙县|