數字
- 整型號
20
0
0xff //十進制為255
//不要使用八進制
- 浮點型(float)
3.14
.3
1.4e-10// 1.4 × 10^-10
- 運算
//+ - * / % 略
console.info(Infinity);//最大數字
console.info(Infinity+1);//最大數字+1
console.info(-Infinity);//最小數字
console.info(-Infinity-1);//最小數字-1
console.info(0/0);//返回NaN
console.info(Math.pow(-2));//返回NaN
溢出:超過了數字上限或者數字下限,返回Infinity;
下溢:無限趨近于0,返回0或者-0;
0/0為NaN(非數字值)
0 = -0
- 浮點數運算的舍入問題
console.info(.3-.2);//0.3-0.2
console.info(.2-.1);//0.2-0.1
console.info(.3-.2==.2-.1);//返回false
時間
var then = new Date(2017,0,1);//2017.1.1
var later = new Date(2017,0,1,15,20,30);//2017.1.1 15:20:30
var now = new Date();
var elapsed = now - then;//相差的毫秒數
console.info(later.getFullYear());
console.info(later.getMonth());
console.info(later.getDay());
console.info(then.getUTCHours());
console.info(then.getHours());
文本
- 直接量
- 關于換行
"two\nlines";//兩行的字符串
"one\
two\
tree" //這是一行的字符串
- 轉義字符\
console.info("A\tA");//水平制表符
console.info("A\vA");//垂直制表符
console.info("A\oA");//NUL字符
console.info("A\bA");//退格符
console.info("A\nA");//換行符
console.info("A\\A");//\
console.info("A\'A");//'
console.info("A\"A");//"
console.info("A\rA");//回車符
布爾值
所有js的值都能轉換成布爾值
undefined;
null;
0;
-0;
NaN
'';
//以上為false,其余為true
if(obj == null){
}
//等價于
if(obj){
}
null 和undefined
null==undefined //返回true;
null===undefined //返回false;
console.info(typeof undefined )//返回undefined
console.info(typeof null)//返回object
undefined是更深層次的“空值” es3中,undefined可讀可寫,es5中是只讀的
不可變的原始值和可變的對象引用
- 全局對象
包括了全局屬性,全局函數,構造函數,全局對象 - 不可變的原始值
var s = 'hello';
s.toUpperCase();//HELLO
console.info(s);//hello
//對象和數組是可變的
var o = {x:1};
o.x=2;
o.y=3;
console.info(o.y);//3
console.info(o.x);//2
- 可變的對象引用
var a = [];
var b = a;
b[0] = 1;
console.info(b[0]);//1
console.info(a[0]);//1
console.info(a===b);//true
類型轉換
值 | 轉為字符串 | 轉為數字 | 轉為布爾值 | 轉為對象 |
---|---|---|---|---|
undfined | "undfined" | NaN | false | throw TypeError |
null | "null" | 0 | false | throw TypeError |
true | "true" | 1 | new Boolean(true) | |
false | "false" | 0 | new Boolean(false) | |
"" | 0 | false | new String("") | |
"1.2" | 1.2 | true | new String("1.2") | |
"one" | NaN | true | new String("one") | |
NaN | "NaN" | false | new Number(NaN) | |
0或者-0 | "0" | false | new Number(0)或new Number(-0) | |
Infinity | "Infinity" | true | new Number(Infinity) | |
{}對象 | toString方法 | valueOf或toString后轉換或拋出異常 | true | |
[]空數組 | "" | 0 | true | |
[9]一個數字 | "9" | 9 | true | |
[1,2]其他數組 | join方法 | NaN | true | |
function(){} | 轉為函數代碼 | NaN | true |
console.info(Number("17"));//17
console.info((17).toString(16));//11
console.info((17.155).toFixed(0));//17
console.info(parseInt('12 哈哈哈'));//12
console.info(parseInt('12,1,拉拉拉'));//12
console.info(parseInt('12,1,阿拉拉拉',16));//18 轉為16進制
非日期對象轉為原始值的基本原則
轉為字符串:
1.有toString方法,用toString();
2.沒有toString方法,若有valueOf(),使用valueOf()后,轉為字符串
3.沒有toString(),沒有valueOf(),報錯轉為字符串:
1.有toString方法,用toString();
2.沒有toString方法,若有valueOf(),使用valueOf()后,轉為字符串
3.沒有toString(),沒有valueOf(),報錯
轉為數字:
1.有valueOf方法,用valueOf();
2.沒有valueOf方法,若有toString(),使用valueOf()后,轉為字符串
3.沒有toString(),沒有valueOf(),報錯
日期對象的特殊性
var now = new Date();
console.info(typeof (now + 1));//字符串
console.info(typeof (now - 1));//數字
console.info(now == now.toString());//true
console.info(now == now.valueOf());//false
console.info(now >(now-1));//true
//在 + == !=時候,轉為string
//在 - > <時候轉為number
變量聲明
若重復聲明,沒有關系,合法無害
若嘗試讀取一個未聲明的變量,報錯
作為屬性的變量是可以被刪除的
var a = 1;
b =2;
this.c = 3;
delete a;//刪除失敗
delete b;//刪除成功
delete c;//刪除成功
變量作用域
- 局部變量優先級高于全局變量
function checkScope() {
console.info(scope);//輸出undefined
var scope = 'local';//盡管變量在這里才有了初值,但是scope在整個函數內都有定義
console.info(scope);//輸出local
return scope;
}
var scope = "global";
- 實際上作用域鏈是存在的,類似于一個堆棧,這個理論有助于理解js的閉包