向下取整:var a = ~~1.2; //1
這個補充一下,還可以用位右移符>> var a = 3.4>>0; //3
但是兩者最好都只用在正整數上,因為只是舍掉了小數部分。Math.floor(-1.2)應該為-2,這兩種方法的結果為-1轉數字var time = + new Date();
設預設值function foo(bar){
var foobar = bar || "default";
//bar 為 undefined, null, "", 0, false, NaN 時最後都得到"default"
}
//坑
[]||"aa"; //[]
{}||"aa"; //SyntaxError
({})||"aa";//Object {}
NaN的坑//NaN - 不是一個數字
isNaN(a);
//檢查是不是 "不是一個數字" ..
isNaN(null);//false
//實際上是null被轉為0了,表面上"null 不是一個 不是數字 的東西"。wtf...
UNICODE 用作變數名var \u4f60\u597d = "\u4f60\u597d";
var b = {};
b.\u4f60\u597d = \u4f60\u597d;
console.log(b);//Object {你好: "你好"}
console.log(b.你好);// "你好"
console.log(b.\u4f60\u597d);// "你好"
console.log("你"==="\u4f60");//true
陣列傳遞和複製var a = [1,2,3];
var b = a;
delete b[1];
console.log(a);//[1, undefined × 1, 3]
var a = [4,5,6];
var b = a.slice(0);
console.log(a);//[4,5,6]
console.log(b);//[4, undefined × 1,6]
物件與Functionconsole.log(typeof Function);//"function"
console.log(typeof Object);//"function"
函式宣告aa();
function aa(){return true;} //true
bb();
var bb = function(){return true;} //TypeError
toString()2.toString();//SyntaxError
2 .toString(); //"2"
2..toString(); //"2"
(2).toString(); //"2"
[1,[2,"abc","",0,null,undefined,false,NaN],3].toString();
//"1,2,abc,,0,,,false,NaN,3"
for in 暴露原型鏈屬性Object.prototype.foo = 1;
var obj = new Object();
obj.bar = 1;
for(var p in obj){
console.log(p);//bar,foo 都遍歷出來了。可以用hasOwnProperty()過濾原型鏈屬性
switch代替if else switch (true) {
case (a > 10):
do_something();
break;
case (a < 100):
others();
default:
;
};
向下取整:var a = ~~1.2; //1
這個補充一下,還可以用位右移符>> var a = 3.4>>0; //3
但是兩者最好都只用在正整數上,因為只是舍掉了小數部分。Math.floor(-1.2)應該為-2,這兩種方法的結果為-1轉數字var time = + new Date();
設預設值function foo(bar){
var foobar = bar || "default";
//bar 為 undefined, null, "", 0, false, NaN 時最後都得到"default"
}
//坑
[]||"aa"; //[]
{}||"aa"; //SyntaxError
({})||"aa";//Object {}
NaN的坑//NaN - 不是一個數字
isNaN(a);
//檢查是不是 "不是一個數字" ..
isNaN(null);//false
//實際上是null被轉為0了,表面上"null 不是一個 不是數字 的東西"。wtf...
UNICODE 用作變數名var \u4f60\u597d = "\u4f60\u597d";
var b = {};
b.\u4f60\u597d = \u4f60\u597d;
console.log(b);//Object {你好: "你好"}
console.log(b.你好);// "你好"
console.log(b.\u4f60\u597d);// "你好"
console.log("你"==="\u4f60");//true
陣列傳遞和複製var a = [1,2,3];
var b = a;
delete b[1];
console.log(a);//[1, undefined × 1, 3]
var a = [4,5,6];
var b = a.slice(0);
delete b[1];
console.log(a);//[4,5,6]
console.log(b);//[4, undefined × 1,6]
物件與Functionconsole.log(typeof Function);//"function"
console.log(typeof Object);//"function"
函式宣告aa();
function aa(){return true;} //true
bb();
var bb = function(){return true;} //TypeError
toString()2.toString();//SyntaxError
2 .toString(); //"2"
2..toString(); //"2"
(2).toString(); //"2"
[1,[2,"abc","",0,null,undefined,false,NaN],3].toString();
//"1,2,abc,,0,,,false,NaN,3"
for in 暴露原型鏈屬性Object.prototype.foo = 1;
var obj = new Object();
obj.bar = 1;
for(var p in obj){
console.log(p);//bar,foo 都遍歷出來了。可以用hasOwnProperty()過濾原型鏈屬性
}
switch代替if else switch (true) {
case (a > 10):
do_something();
break;
case (a < 100):
others();
break;
default:
;
break;
};