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]”
相关文章推荐
- JS中创建函数的三种方式及区别
- ADF运行jsp的时候出现 JSP scriptlets will not work properly if using OJSP
- javascript实例——文本特效篇(包含3个小例子)
- JSR303数据校验
- javascript——base64
- JavaScript绑定事件的方法[3种]
- d3.js——集群图的绘制
- POST+JSON+XML
- JavaScript原型与原型链学习笔记
- Javascript模块化编程
- jsp页面之间用?传值
- javascript知识点记录(2)
- [javascript] 看知乎学习js闭包
- 发布项目,加入CRUD功能
- js修改浏览器url
- JS树-深度遍历和广度遍历
- javascript的this的三种使用方式
- JS二叉树的遍历(前序,中序,后序)
- 读书时间《JavaScript高级程序设计》五:DOM
- extremecomponents在web中的使用