TypeScript var VS let (2)

var

ts

function sumMatrix(matrix: number) {
    var sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        var currentRow = matrix;
        for (var i = 0; i < currentRow.length; i++) {
            sum += currentRow;
        }
    }

    return sum;
}
TypeScript var VS let (1)

var

for (var i = 0; i < 10 ; i++) {
    setTimeout(function() {console.log(i); }, 100 * i);
}
TypeScript let

Block-scoping

支持块级作用域

编译出错:

error TS2304: Cannot find name 'b'.

function f(input: boolean) {
    let a = 100;

    if (input) {
        // Still okay to reference 'a'
        let b = a + 1;
        return b;
    }

    // Error: 'b' doesn't exist here
    return b;
}
TypeScript const

const numLivesForCat = 9;
const kitty = {
    name: "Aurora",
    numLives: numLivesForCat,
}

// Error
kitty = {
    name: "Danielle",
    numLives: numLivesForCat
};

// all "okay"
kitty.name = "Rory";
kitty.name = "Kitty";
kitty.name = "Cat";
kitty.numLives--;
TypeScript 基本类型

TS 代码

点击查看代码
// 基本类型
// 布尔类型
let isDone: boolean = false;
// 数值类型
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;
// 字符类型
let color: string = "blue";
name = 'red';
// 多行文本
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ name }.

I'll be ${ age + 1 } years old next month.`
// 数组类型
let list: number = [1, 2, 3];
let lista: Array<number> = [1, 2, 3];
// 元组类型
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK
// Initialize it incorrectly
// x = [10, 'hello']; // Error
console.log(x[0].substr(1)); // OK
// console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
x[3] = 'world'; // OK, string can be assigned to (string | number)
console.log(x[3].toString()); // OK, 'string' and 'number' both have toString
//x[6] = true; // Error, boolean isn't (string | number)
// 枚举
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
// 可以自定义枚举项的开始值
enum ColorA {Red = 1, Green, Blue};
let ca: ColorA = ColorA.Green;
// 也可以自定义所有枚举项的值
enum ColorB {Red = 1, Green = 2, Blue = 4};
let cb: ColorB = ColorB.Green;
// 可以根据值 map 到对应的名字
enum ColorC {Red = 1, Green, Blue};
let colorName: string = ColorC[2];
console.log(colorName);
// Any 类型
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
// 
let notSureA: any = 4;
notSureA.ifItExists(); // okay, ifItExists might exist at runtime
notSureA.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
// prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
// 可能包含多种类型的数组
let listAny: any = [1, true, "free"];
listAny[1] = 100;
// void 与 any 相反,没有任何类型
function warnUser(): void {
    alert("This is my warning message");
}
// void 类型只能设置为 undefined 或者 null
let unusable: void = undefined;
// 类型断言 (尖括号)
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
// 类型断言 (as)
let someValue2: any = "this is a string";
let strLength2: number = (someValue2 as string).length;
// 关于 let
// 使用 let 代替 var 创建变量,可以减少一些 js 中问题