酱油JS分享第二讲_数据类型与操作符说课.ppt
文本预览下载声明
by Bosn
二、玩转数据类型
《JS公开课》系列分享1). 认识JavaScript DONE2). 数据类型 操作符3). 谈对象4). 基于原型的继承机制5). 运行上下文6). 神奇的闭包7). 高性能JavaScript
声明
关于默认为ES5标准的限定
关于默认用例环境为V8引擎的限定
关于本PPT的备注
1
Looking Back
5 – “4”
5 + “4”
+!{}[true]
+[1]
+[1, 2]
7 – “a”
7 / 0
…
5 + “4”
5 + null
4 == “4.00”
4 === “4.00”
null == undefined
0 == false
0 == null
null == false
2
Basic
var num = 32;
num = “this is a string”;
动态弱类型
number
string
boolean
null
undefined
基元类型
object
基元类型
Primitive Type
不变的
Immutable
64位双精度二进制格式IEEE754值
double-precision 64-bit binary format IEEE 754 value
不变的字符串
Immutable String
类型数组
Typed Array
WeakMaps, Maps, Sets
Khrono Working Draft
+ -
var x = ‘The answer is ‘ + 42;
var y = 42 + ‘ is the answer’;
“37” – 7
“37” + 7
// 30
// 377
num - 0
num + ‘’
加减
巧用加减转换类型
== ===
== 和 ===
类型判断
类型转换
4 == “4.00” //true
4 === “4.00” // false
* /
var x = ‘32‘ * 42;
var y = 42 / ‘ 32’;
true * 100
false / 1
乘除
NaN
任一操作数是NaN结果为NaN
Infinity * 0结果为NaN
Infinity
Infinity * Infinity结果为Infinity
1 / 0结果为Infinity
-1 / 0 结果为-Infinity
Infinity / Infinity结果为NaN
3
Wrapper Object
var a = “string”;
alert(a.length);
a.t = 3;
alert(a.t);
4
Type Detection
几种类型检测
typeof(null) === “object”
typeof( [1, 2] ) === “object”
typeof(new Object() ) === “object”
typeof(NaN ) === “number”
typeof 100 === “number”
typeof true === “boolean”
typeof function () {} === “function”
Why typeof null === “object”
检测null
obj === null
检测NaN
NaN === NaN; // false
isNaN(NaN); // true
isNaN(42); // false
Instanceof的使用
[1, 2] instanceof Array === true
new Object() instanceof Array === false
IE6/7/8 Object.prototype.toString.apply(null) 返回’[object Object]’
Object.prototype.toString.apply([]); === “[Object Array]”;
Object.prototype.toString.apply(function(){}); === “[Object Function]”;
Object.prototype.toString.apply(null); === “[Object Null]”;
类型检测实践
类型检测小结
typeof
适合基元类型及function检测,遇到null完蛋。
[[Class]]
通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined完蛋(IE678等返回[object Object])。
in
显示全部