JavaScript 基礎概念
變數
都使用 var (JS ES6 後有像是 let, const 等其他關鍵字, 但本案相關開發請都用 var)
資料型態
- 主要的原始資料類型:
- 數字(Number)
- 字串(String)
- 布林(Boolean)
- 特殊的原始資料類型:
- 空(null)
- 未定義(undefined)
- 符號(Symbol)
- 複合型(composite)或參考型(reference)的資料類型:
https://jsbin.com/hafejilazo/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| console.log(typeof 37); console.log(typeof NaN); console.log(typeof ''); console.log(typeof (typeof 1)); console.log(typeof true); console.log(typeof null); console.log(typeof function(){});
console.log(a); console.log(typeof(a)); var a = 100; console.log(a); console.log(typeof(a));
var aBool = true; console.log(aBool);
var bBool = !aBool; console.log(bBool);
var cBool = !!aBool; console.log(cBool);
var aBool = !!0; console.log(aBool);
var bBool = !!'false'; console.log(bBool);
var cBool = !!NaN; console.log(cBool);
|
Double Equals vs. Triple Equals
請盡量用 === 或 !== 來比較值
https://jsbin.com/nahuqojote/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| console.log(0 == false);
var a = 3 + 4 * 5; console.log(a)
console.log(3 < 2 < 1);
|
自動轉型
- string 做加法運算, 自動轉型成 string
- 減乘除運算 自動轉型 number
https://jsbin.com/cagoyenovi/1/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| var a = 1 + '2'; console.log(a) console.log(typeof(a)); console.log("=================================================");
var b = 'ithome' + 30; console.log(b); console.log(typeof(b)); console.log("=================================================");
var c = '10' * 8 console.log(c); console.log(typeof(b)); console.log("=================================================");
var d = '10' - 8; console.log(d); console.log(typeof(d)); console.log("=================================================");
var f = 'ithome' - 30; console.log(f); console.log(typeof(f)); console.log("=================================================");
|
- if( ) 判斷式
- 0 , NaN , ‘’ , null , undefinied 自動轉型 false
- 其餘皆自動轉型 true : 有值的狀態下, {}, [], ’ ’ 等等…
https://jsbin.com/bozizitanu/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
var f = 'ithome' - 30; console.log(f);
if(f) { console.log('pass'); }
var b; if(!b) { console.log('pass'); }
if('3332') { console.log('pass'); }
var c = 0; if(c) { console.log('pass'); }
|
運算子優先序
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
https://jsbin.com/nahuqojote/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| console.log(0 == false);
var a = 3 + 4 * 5; console.log(a)
console.log(3 < 2 < 1);
|
js 裡稀奇古怪靈異事件一大堆
•http://zero.milosz.ca/
•http://wtfjs.com/
陣列
https://www.w3schools.com/js/js_arrays.asp
物件
1
| {name: 'Simon',isF2E: true}
|
https://jsbin.com/xezuyikigo/edit?js,console
https://www.w3schools.com/js/js_objects.asp
函式(function)就是物件
在JS這個物件導向語言裡,其函式的特性被稱為一級函式。
一級函式First Class Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| function() {
}
function sport() {
}
因為函式就是物件,所以我們可以接著這樣寫
sport.basketball = '打籃球'; console.log(sport.basketball);
JS 函式就是物件,物件可以自由擴增屬性,我們等於對 sport 這個函式物件, 新增一個 basketball 屬性,其值為字串打籃球。
所以也可以寫成這樣, 函式物件 var sport = function(a) {
}
sport(3);
|
https://jsbin.com/gocofesiha/edit?js,console
https://jsbin.com/wozilisedo/1/edit?js,console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| var sport = function(a) { console.log(a); }
sport('test');
function a() { console.log(a);
}
log(a);
log(function a() { console.log(a); });
function logf(a){ a(); } logf(function(){ console.log("狗"); })
var sport = function(a) { console.log(a); }
sport('test');
function log(a){ console.log(a); }
log(sport('test'));
|
Hoisting 提升
函式 與其 函式 內容(定義):會存至記憶體,所以可以正確的呼叫,呼叫後建立這個函式的執行環境
變數(var): 也會被創造存至記憶體,但其賦值內容並沒有再創造時跟著被進去
1 2 3 4 5 6 7 8
| var a = 'Hello World!';
function b() { console.log('Called b!'); }
b(); console.log(a);
|
https://jsbin.com/cahapugisa/edit?js,console
v.s.
1 2 3 4 5 6 7 8
| b(); console.log(a);
var a = 'Hello World!';
function b() { console.log('Called b!'); }
|
https://jsbin.com/nujawaqidi/edit?js,console
ex1:
1 2 3 4 5
| cat()
function cat(){ console.log("貓咪"); }
|
ex2:
1 2 3 4 5
| var dog = function(){ console.log("狗"); }
dog();
|
ex3:
1 2 3 4 5
| dog();
var dog = function(){ console.log("狗"); }
|
loop
1 2 3
| for (var count = 0 ; count < 10 ; count++){ console.log(count) }
|
References: