Created
July 25, 2016 15:03
-
-
Save winwu/cbbd25d368fc743a65006aa99237cfec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// boolean 布林 | |
var isUserExists = false; | |
// Number 數值 | |
var myAge = 26; | |
// String 字串 | |
var jobTitle = 'Frontend Engineer'; | |
// String 也可以用 字串樣板 (Template Literals) ${ expr } | |
var API_STEM = 'http://api.xxx.xx?'; | |
var API_KEY = 'SK935232fwgw'; // 我亂打的 示意一下而已 | |
var API_URL = API_STEM + "KEY=" + API_KEY; | |
// Array 陣列 | |
// 如數字型別的陣列 | |
var luckyNumber = [12, 17, 27, 46, 33, 11]; | |
// 也可以用 泛型 來表示一個 Array Type | |
var list = [12, 17, 27, 46, 33, 11]; | |
// Tuple 元組,定義一個固定數量且已知型別的 array | |
// 定義一個 Tuple | |
var userNameAndAge; | |
// 正確的初始化 init 示範: | |
userNameAndAge = ["win wu", 520]; | |
/* 錯誤的初始化像是故意把數字跟字串相反: | |
* userNameAndAge = [520, "winwu"]; | |
* 會得到錯誤訊息 | |
* basic.ts(27,1): error TS2322: | |
* Type '[number, string]' is not assignable | |
* to type '[string, number]'. | |
*/ | |
// Enum 列舉 | |
var Book; | |
(function (Book) { | |
Book[Book["PHP"] = 0] = "PHP"; | |
Book[Book["ROR"] = 1] = "ROR"; | |
Book[Book["Node"] = 2] = "Node"; | |
})(Book || (Book = {})); | |
; | |
var p = Book.PHP; | |
/* 預設情況下 Enum 第一個 index 是 0 | |
* 但你可以調整,若希望第一個從 1 開始: | |
*/ | |
var Color1; | |
(function (Color1) { | |
Color1[Color1["Pink"] = 0] = "Pink"; | |
Color1[Color1["Yellow"] = 1] = "Yellow"; | |
Color1[Color1["Green"] = 2] = "Green"; | |
})(Color1 || (Color1 = {})); | |
; | |
var colorName1 = Color1[1]; | |
alert(colorName1); // print “Yellow” | |
var Color2; | |
(function (Color2) { | |
Color2[Color2["Pink"] = 1] = "Pink"; | |
Color2[Color2["Yellow"] = 2] = "Yellow"; | |
Color2[Color2["Green"] = 3] = "Green"; | |
})(Color2 || (Color2 = {})); | |
; | |
var colorName2 = Color2[1]; | |
alert(colorName2); // print “Pink” | |
// Any 任意型別 可以調用任意方法 | |
/* 有時候我們在開發的時候可能還無法決定該變數的型別 | |
* 或是這個資料的內容是動態的 | |
*/ | |
var notSureData = 4; | |
notSureData = false; | |
// Void 無資料 return 時或空值 | |
var unUseable = undefined; | |
function doNothing() { | |
return undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment