typeof能正确区分原始值吗
类型
优点:能够快速实现基本数据类型类型的缺点:不能将对象、数组和空变量,都返回对象
-
typeof
的作用?数据类型,可以返回7种数据类型:
number、string、boolean、undefined、object、function
,以及ES6
新增的symbol
-
typeof
能正确的数据类型吗?不能对于原始类型,除。
null
都可以正确判断;对于引用类型,除function
外,都会返回"object"
-
typeof
注意事项typeof
返回是形式string
,注意类似考题:typeof(typeof(undefined)) -> "string"
typeof
未定义的变量不会报错,返回"undefiend"
typeof(null) -> "object"
: 遗留已久的bug
typeof
无法区别的数组与普通对象:typeof([]) -> "object"
typeof(NaN) -> "number"
实例
优点:能够数组类型、对象和函数,适合用于判断自定义的类实例对象 缺点:数字,布尔,字符串基本数据不能判断
instanceof
判断对象的原型链上是否存在构造类型的原型。instanceof
常用来判断A
是否为B
的实例
Object.prototype.toString.call()
优点:判断数据类型 叙述:写法繁难记,推荐进行包装后使用
到字符串。call ( ( ) => { } ) // [对象函数]
toString . call ( { } ) // [object Object]
toString . call ( [ ] ) // [对象数组]
toString . call ( '' ) // [对象字符串]
toString . call ( 22 ) // [对象编号]
toString . call ( undefined ) // [对象未定义]
到字符串。call ( null ) // [object null]
toString . call ( new Date ) // [对象日期]
toString . call ( Math ) // [object Math]
toString . call ( window ) // [对象窗口]
typeof的返回值有哪些
typeof function 会显示什么
typeof为什么对null错误的显示
这只是 JS 存在的一个悠久的错误。在 JS 的最初版本中使用的是 32 位系统,为了性能考虑使用低位变量的类型信息,000 开头代表是对象,但空表示为全零,所以将它错误的判断为对象
typeof('abc')和 typeof 'abc'都是 string, 那么 typeof 是操作符还是函数?
操作符
作者:战场小包
链接:https://juejin.cn/post/7028478428680552456