您的位置:首页 > Web前端 > JavaScript

JavaScript 深入浅出 1 数据类型

2016-04-26 17:00 609 查看
<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;">1.六种数据类型 原始类型 :number   string   boollean   null   undefined
object对象 :Function   Array   Data
2.隐式转换  “  +   -   ” </span></span>
<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;">    var x = ‘The answer is ‘ + 42;  </span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><strong>    var y = 42 + ‘ is the answer’
</strong>  注释:  巧用 +/-  规则转换类型  num - 0   num + ""      eg : "37" - 7    //30
"37" + 7     //377
<strong>等于  “ a == b ”</strong> ;  0 == false   null == undefined
类型相同,同===    类 型不同,尝试类型转换和比较:    null == undefined 相等    number == string 转number 1 == “1.0" // true    boolean == ? 转number 1 == true // true    object == number | string 尝试对象转为基本类型 new String('hi') == ‘hi’ // true    其它:false</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;">
</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><strong>严格等于 “ a === b ”</strong>  类型不同:返回false ;类型相同  :null === null     undefined === undefined     特殊:数值 NaN ≠ NaN 自己比较不相等  、对象  new Object  ≠ new Object  用值比较 而不是对象</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><strong>
3.包装对象var str = "string"
alert(str.length)      //6</strong></span></span>
<span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:12px;">str.t = 4
alert(str.t)           //undefined
</span></span>
<span style="font-size:12px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; background-color: rgb(255, 255, 255);">
</span>
<span style="font-size:12px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; background-color: rgb(255, 255, 255);">4.类型检测</span>


typeof :适合基本类型及function检测,遇到null失效。

[[Class]] :通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined失效(IE678等返回[object Object])。

instanceof :适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效
</pre><pre name="code" class="plain">typeof 100 === “number”
typeof true === “boolean”
typeof function () {} === “function”
typeof(undefined) ) === “undefined”
typeof(new Object() ) === “object”
typeof( [1, 2] ) === “object”
typeof(NaN ) === “number”
typeof(null) === “object”


[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]”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: