自動拆分字符串
function test(templage, name, age) {
console.log(templage);// ["hello,my name is ", ",i am ", ""]
console.log(name);// Aaayang
console.log(age);// 18
}
var myname = 'Aaayang';
var getAge = function() {
return 18;
};
test`hello,my name is ${myname},i am ${getAge()}`;
參數(shù)類型
// 參數(shù)類型:在參數(shù)名稱后面使用冒號來指定參數(shù)的類型
var myname: string = "Aaayang";
myname = "Bbb";
// 類型推斷機(jī)制
var alias = "Jiangjun";
// 推斷這里應(yīng)該是字符串大溜,賦值數(shù)字會報(bào)錯
alias = 'a';
// 任意類型
var aaa: any = "Doudou";
// aaa賦值啥都行
aaa = 13;
var age: number = 13;
var man: boolean = true;
// 不需要任何返回值
function test() :void {
}
// 要返回string
function test2() :string {
return "";
}
function test3(name: string) :string {
return "";
}
// 只能傳字符串
test3('aaa');
// 自定義類型
class Person {
name: string;
age: number;
}
var zhangsan: Person = new Person();
zhangsan.name = "zhangsan";
zhangsan.age = 18;
參數(shù)默認(rèn)值
var myname: string = "Aaayang";
// 注意帶默認(rèn)值的參數(shù)要聲明在后面
function test(a:string, b:string, c:string = 'Doudou') {
console.log(a);
console.log(b);
console.log(c);
}
// 正常只有傳3個參數(shù)才不會報(bào)錯,第三個參數(shù)有默認(rèn)值,這里傳2個也行
test('111', '222', '333');
test('xxx', 'yyy');
可選參數(shù)
var myname: string = "Aaayang";
// 注意可選參數(shù)必須聲明在必選參數(shù)的后面
function test(a:string, b?:string, c:string = 'Doudou') {
console.log(a);
console.log(b);
console.log(c);
}
// 正常只有傳3個參數(shù)才不會報(bào)錯傍念,第三個參數(shù)有默認(rèn)值,這里傳2個也行
test('xxx');// xxx,undefined,Doudou
test('aaa', 'bbb');// aaa,bbb,Doudou
test('111', '222', '333');// 111,222,333
Rest and Spread
// 把任意數(shù)量參數(shù)轉(zhuǎn)為數(shù)組
function func1(...args) {
args.forEach(function(arg) {
console.log(arg);
});
}
func1('a', 'b', 'c', 'd');
// 把任意數(shù)量參數(shù)轉(zhuǎn)為數(shù)組
function func1(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
var args = [1, 2];
func1(...args);// 1,2,undefined
var args2 = [6, 7, 8, 9];// 6,7,8
func1(...args2);
Generator方法
function* doSomething() {
console.log("start");
yield;
console.log("finish");
}
var func1 = doSomething();
func1.next();// start
func1.next();// finish
yield寫法研究
function* getStockPrice(stock) {
while(true) {
yield Math.random() * 100;// ??
}
}
var priceGenerator = getStockPrice("IBM");
var limitPrice = 15;
var price = 100;
while(price > limitPrice) {
price = priceGenerator.next().value;
console.log(`the generator return ${price}`);
}
console.log(`buying at ${price}`);
解構(gòu)賦值
function getStock() {
return {
code: "IBM",
price: 100
}
}
// var stock = getStock();
// var code = stock.code;
// var price = stock.price;
var {code, price} = getStock();
var {code: codex, price} = getStock();
console.log(code);// IBM
console.log(codex);// IBM
console.log(price);
箭頭函數(shù)
// 單行
var sum = (arg1, arg2) => arg1 + arg2;
console.log(sum(1, 2));
// 多行
var sum2 = (arg1, arg2) => {
var result = arg1 + arg2;
console.log(result);
}
sum2(3, 4);
var sum3 = () => {
console.log('無參數(shù)的');
}
sum3();
var sum4 = arg1 => {
console.log(`只有一個參數(shù)也是不需要寫括號的${arg1}`);
}
sum4('Aaayang');
var myArray = [1, 2, 3, 4, 5];
var aaa = myArray.filter(item => item % 2 == 0);
console.log(aaa);
箭頭函數(shù)中的this
// 正常寫
function getStock(name:string) {
this.name = name;
setInterval(function() {
console.log("my name is " + this.name);
}, 1000);
}
var stock = new getStock('Aaayang');// my name is
// 箭頭函數(shù)寫法
function getStock2(name:string) {
this.name = name;
setInterval(() => {
console.log("my name is " + this.name);
}, 1000);
}
var stock2 = new getStock2('Aaayang');// my name is Aaayang
forEach, for in, for of
var myArray = [1, 2, 3, 4];
myArray.desc = "four number";// tsc編譯會報(bào)錯,便宜JS是可以執(zhí)行的
// forEach會忽略desc,不能用break跳出循環(huán)
myArray.forEach(value => console.log(value));
// desc屬性也會被循環(huán)
for(var n in myArray) {
// console.log(n);// 0, 1, 2, 3, desc
console.log(myArray[n]);// 1, 2, 3, 4, four number
}
// 可以break钝吮,forEach一樣不會循環(huán)desc
for(var n of myArray) {
if(n > 2) break;
console.log(n);
}
類
class Person {
name;// 默認(rèn)public
// private name;// 私有的
// protected name;// 內(nèi)部和子類可以訪問
eat() {
console.log("i am eating");
}
}
var p1 = new Person();
p1.name = "Aaayang";
p1.eat();
var p2 = new Person();
p2.name = "Jiangjun";
p2.eat();
構(gòu)造函數(shù)
class Person {
/*constructor(public name: string) {
}*/
// 和上面等價
constructor(name: string) {
this.name = name;
}
name;
eat() {
console.log(this.name);
}
}
var p1 = new Person('Aaayang');// 必須傳參
p1.name = "Aaayang";
p1.eat();
繼承
class Person {
constructor(public name: string) {
console.log('haha');
}
name;
eat() {
console.log('i am eating');
}
}
class Employee extends Person {
constructor(name: string, code: string) {
super(name);// 子類的構(gòu)造函數(shù)必須調(diào)用父類的構(gòu)造函數(shù)
console.log('xixi');
this.code = code;
}
code: string;
work() {
super().eat();// 調(diào)用方法
this.doWork();
}
private doWork() {// e1.doWork()不讓直接干活,規(guī)定先吃飯?jiān)俑苫? console.log('do work');
}
}
var e1 = new Employee("Aaayang", "1");
e1.work();
泛型
...
接口
interface IPerson {
name: string;
age: number;
}
class Person {
// 接口:作為方法的參數(shù)的類型聲明
constructor(public config: IPerson) {
}
}
var p1 = new Person({
name: "Aaayang",
age: 18
});
interface Animal {
eat();
}
class Sheep implements Animal {
eat() {
// 類必須要實(shí)現(xiàn)接口對應(yīng)的方法
console.log('i eating grass');
}
}
模塊
a.ts
export var prop1;
export function func1() {
}
export class Class1 {
}
index.ts
import {prop1, func1, Class1} from "./a";
注解
類型定義文件
// 在TypeScript中使用已有的工具包
// from github "DefinitelyTyped"
*.d.ts