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

先知JS分享第二讲 数据类型与操作符

2014-01-15 23:57 666 查看
感谢Bosn的分享

玩转数据类型

《JS公开课》系列分享

1). 认识JavaScript DONE

2).数据类型 & 操作符

3). 谈对象

4). 基于原型的继承机制

5). 运行上下文

6). 神奇的闭包

7). 高性能JavaScript

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



大家众所周知,JS分为以上两种类型,一种是对象,另外一种是基元类型。

var x = ‘The answer is ‘ + 42;
var y = 42 + ‘ is the answer’;
“37” – 7   //30
“37” + 7  //'377'


最常用且容易疑惑的是加法运算,除了算数意义,还表示着字符串拼接。

上面的代码就可以明显看出来,



细心看下这一段ECMA的标准

二元加法操作符“+”:任意一个操作数是字符串,理解为字符串拼接。

二元减法操作符“-”:操作数尝试转换成数字并做减法运算。

11.9.3 The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as
follows:
1. If Type(x) is the same as Type(y), then
a. If Type(x) is Undefined, return true.
b. If Type(x) is Null, return true.
c. If Type(x) is Number, then
i. If x is NaN, return false.
© Ecma International 2011 81
ii. If y is NaN, return false.
iii. If x is the same Number value as y, return true.
iv. If x is +0 and y is -0, return true.
v. If x is -0 and y is +0, return true.
vi. Return false.
d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same
length and same characters in corresponding positions). Otherwise, return false.
e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
f. Return true if x and y refer to the same object. Otherwise, return false.
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
4. If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
5. If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
8. If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
10. Return false.


再列一段,ECMA标准规范上面写明了,==进行比较的时候采取怎么处理的方式。
今天晚上先弄到这里,明天继续。把没写完的搞完,包括包装对象Wrapper Object

3、Wrapper Object

var a = “string”;
alert(a.length); //6
a.t = 3;
alert(a.t); //undefined


定义的a是一个字符串,本身是没有length这个方法的,为什么弹出的会是6呢?难道转换成了Object?

但是后面的alert弹出的却是undefined,证明了a添加t方法失败,那就是说a不是一个Object。为什么会出现这个区别呢?先看下图



实际上,在JS处理这个问题的时候有自己的处理方式。

var a = “string”;
alert(a.length);//当处理这个a.length的时候,实际上经过了以下步骤
//这个tmp是虚构的,方便大家理解
//var tmp = new String(a);
//tmp.length;//6
//a.length实际上是tmp.length
//tmp是一个String对象有length这个方法
//然后处理完了之后tmp就被销毁了
//所以a还是一个String


苦B的在公司加班中。。。下一条,其实也是一个思想。

a.t = 3;
alert(a.t); //这里的处理方式跟上面的其实是一样的
//var tmp = new Object(a);
//tmp.t = 3;实际上添加了t这个方法,但是处理完这一条之后,被销毁。
//所以alert(a.t)这个时候的a还是一个字符串。没有t方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: