1樓:A-yon
obj === null || [『string』, 『number』, 『boolean』, 『symbol』, 『bigint』].includes(typeof obj)
undefined 可以忽略
2樓:papa pa
原始型別不該是 string number boolean null undefined symbol bigint 嗎……
const
isPrimitive
=wtf
=>typeof
wtf===
'object'?!
wtf:
typeof
wtf!==
'function'
什麼你嫌長?
const
isPrimitive
=wtf
=>Object
(wtf
)!==
wtf// 前提是 Object 不能被修改這個夠短嗎?
可參考:
ecma-international.org/ecma-262/6.0/#sec-object-value以及隔壁 @葉落偏執 寫的是個什麼……提供反例幾個:
// false negative
const
example1
=null
// typeof null === 'object'
const
example2
=NaN
// Object.is(NaN - NaN, NaN)const
example3
='a'
// 同上
const
example4=0
n// 0n - 0n === 0n && 0n !== 0// false positive
const
example5
=Object
.assign(x
=>0,
)// typeof example5 === 'function'
// throws
const
example6
=Symbol
()// TypeError: Cannot convert a Symbol value to a number
JavaScript裡Date toJSON的意義是什麼?
沈嘯 toJSON方法主要是用來給JSON.stringify呼叫的。你也可以根據自己的需要去過載它 比如這樣 Date prototype toJSON function 或者 Date prototype toJSON function 然後你就可以通過JSON.stringify來獲得包含Da...
javascript 中,for 迴圈裡使用匿名函式好嗎?
James Dork 額,如果單純為了避免汙染命名空間,是不是可以把只在for中用到的變數存成乙個物件的屬性 var loop for loop.i 0 loop.i 100 loop.i 或者存為for所在函式名的屬性 var a function for a.i 0 a.i 100 a.icon...
javascript 元件怎麼寫?
你看看git上的這個寫法,閉包這樣可以把多個方法封裝在乙個物件上,並且變數都在內部定義不會洩漏到外部 我說的範圍窄一些,就是一般意義上我們使用的前端元件,可以看一些元件的原始碼,細細體會,從簡單的入手,一般採用配置初始化,然後繼承的方式增加內部方法,當然也不固定,參考上面回答的,當然具體怎麼寫好看你...