先知JS分享第二講 數據類型與操做符

感謝Bosn的分享閉包

玩轉數據類型app

《JS公開課》系列分享

1). 認識JavaScript DONE

2).數據類型 & 操做符

3). 談對象

4). 基於原型的繼承機制

5). 運行上下文

6). 神奇的閉包

7). 高性能JavaScript性能

 

一、Looking Back

首先作一個回顧spa

5 – 「45 + 「4+!{}[true]
+[1]
+[1, 2]
7 – 「a」
7 / 0
5 + 「45 + null
4 == 「4.004 === 「4.00null == undefined
0 == false
0 == null
null == false


首先各位作一下上面的題目吧,看看本身作出來的和結果是否是同樣。code

作完題目了,先別急,不懂的咱們會在下文講到,懂的就當補腦了,也能夠複習一下。orm

二、Basic

 

你們衆所周知,JS分爲以上兩種類型,一種是對象,另一種是基元類型。對象

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

最經常使用且容易疑惑的是加法運算,除了算數意義,還表示着字符串拼接。blog

上面的代碼就能夠明顯看出來,繼承

細心看下這一段ECMA的標準ip

二元加法操做符「+」:任意一個操做數是字符串,理解爲字符串拼接。

二元減法操做符「-」:操做數嘗試轉換成數字並作減法運算。

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

三、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方法。
相關文章
相關標籤/搜索