原始类型:
number
string
boolean
null
立即学习“Java免费学习笔记(深入)”;
undefined
对象类型:
Object
function
Array
Date
...
"37" + 7 = "377"
"37" - 7 = 30
以下为true:
"1.23" == 1.23
0 == false
null == undefined
类型不同,返回false
类型相同,以下为true:
null === null
undefine === null
NaN != NaN
new Object != new Obejct
类型相同,同===
类型不同,尝试类型转换比较
null == undefined
number == string 转number
boolean == ? 转number
Object == number | string 尝试对象转换为基本类型
其他:false
为了便于操作基本类型值,Js提供了基本类型的自动包装功能,每单读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,并在调用后自动销毁。
由于基本包装类型和基本类型的含义并不一样,会导致typeof等操作产生不同的结果,不推荐显示实例化基本数据类型
var a = "string"; alert(a.length); //6 a.t = 3; alert(a.t); //undefined
以下为true:
typeof 100 === “number” typeof true === “boolean” typeof function () {} === “function” typeof(undefined) ) === “undefined” typeof(new Object() ) === “object” typeof( [1, 2] ) === “object” typeof(NaN ) === “number” //NaN也为number typeof(null) === “object”
obj instanceof Object 利用原型链进行判断,适用于对象间判断。它期望左边是一对象,右边是函数对象或函数构造器。
以下为true:
[1, 2] instanceof Array === true new Object() instanceof Array === false
Object.prototype.toString.apply([]); === “[object Array]”; Object.prototype.toString.apply(function(){}); === “[object Function]”; Object.prototype.toString.apply(null); === “[object Null]” Object.prototype.toString.apply(undefined); === “[object Undefined]” // IE6/7/8 Object.prototype.toString.apply(null) 返回”[object Object]”
typeof
适合基本类型及function检测,遇到null失效。
[[Class]]
通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined失效(IE678等返回[object Object])。
instanceof
适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效。
以上就是分享JavaScript类型之间的比较的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号