返回一个布尔值,确定提供的值是否为原始值。
在不是原始类型的字符串数组上使用 Array.includes()
,
使用 typeof
提供类型。
由于 typeof null
被求值为 'object'
,所以需要直接比较。
const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
isPrimitive(window.someNonExistentProperty); // true isPrimitive(null); // true isPrimitive(50); // true isPrimitive('Hello!'); // true isPrimitive(false); // true isPrimitive(Symbol()); // true isPrimitive([]); // false isPrimitive(new String('Hello!')); // false
更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂