TypeScript 入门

2020-05-31  本文已影响0人  彬_哥哥

话不多说先上图!!!


timg.jpg

TypeScript 的好处

TypeScript 安装开发环境

TypeScript 概念,语法和特性

// 多行字符串
  ``

//js
  var content = 'aa'+
'bb'+
'cc';
//var content = 'aabbcc';

// typescript
var content = `aa
bb
bb`
//var content = "aabbcc";
`${xxxxx}`

var myname = 'zhai liang';
var getName = function () { 
    return 'zhai liang'
}
console.log(`hell ${myname}`);
console.log(`hell ${getName}`);
function test(template, name, age) { 
    console.log(template)
    console.log(name)
    console.log(age)
}

var myname = 'zhai liang';
var getAge = function () { 
    return 18;
}
test`hello my name is ${myname},i m ${getAge()}`

参数新特性

var myname: string = "zhan ling"; // 字符串

var alias:any = 'xixi'; // 可以声明任何类型0

var age: number = 13; // 数字

var man: boolean = true; // 布尔

function test(): void {  // void  不需要任何类型的返回值
    return
}

// 自定义类型
class Person {
    name: string; 
    age:number
}

var zhangsan: Person = new Person();

zhangsan.name = "zhansan";
zhangsan.age = 18;
var myname: string = "zhangliang";
function test(a: string, b: string, c: string) { 
    console.log(a);
    console.log(b);
    console.log(c);
}
test("xxx", "yyy", "zzz");
var myname: string = "zhangliang";
function test(a: string, b?: string, c: string="cese") { 
    console.log(a);
    console.log(b);
    console.log(c);
}
test("xxx");

函数新特性

function func1(...args) {
    args.forEach(function (arg) { 
        console.log(arg)
    })
}

func1(1, 2, 3);
func1(7, 8, 9, 10, 11);

//-----------------------
function func1(a,b,c) {
    console.log(a);
    console.log(b);
    console.log(c);
}

var args = [1, 2]
func1(...args);

var args2 = [7, 8, 9, 10, 11];
func1(...args2);

...
function getStock() { 
    return {
        code: "IMB",
        price:100
    }
}

var { code, price } = getStock();

//------------------------
function getStock() { 
    return {
        code: "IMB",
        price: {
            price1: 100,
            price2:200
        }
    }
}

var { code, price: {price2 } } = getStock();

// ------------------
var array1 = [1, 2, 3, 4]

var [number1, number2,...others] = array1;

console.log(number1);
console.log(number2);
console.log(others);

表达式和循环

var sum = (arg1,arg2) => arg1 + arg2
var sum = arg1 => {
    return arg1
}

// 过滤 复数
var myArray = [1, 2, 3, 4, 5];
console.log(myArray.filter(value => value % 2 == 0));


// 传统方式  this 指向问题
function getStock(name: string) {
    this.name = name;
    setInterval(function () { 
        console.log("name is:"+ this.name)
    },1000)
}

var stock = new getStock("IMB");

// 箭头方式  this 指向问题
function getStock2(name: string) {
    this.name = name;
    setInterval(() => { 
        console.log("name is:"+ this.name)
    },1000)
}

var stock = new getStock2("IMB");
// forEach
var myArray = [1, 2, 3, 4];
myArray.desc = "four number";
myArray.forEach(value => console.log(value))
// 输出值 1,2,3,4


// for in
var myArray = [1, 2, 3, 4];
myArray.desc = "four number";

for (var n in myArray) {
    console.log(myArray[n])
}
// 输出值 1,2,3,4,four number


// for  of
var myArray = [1, 2, 3, 4];
myArray.desc = "four number";

for (var n of myArray) {
    console.log(n)
}
// 输出值 1,2,3,4

TypeScript 特征

class Person {
    name;
    eat() { 
        console.log("im eating")
    }
}

var p1 = new Person();

p1.name = "zhangsna"
p1.eat();

上一篇下一篇

猜你喜欢

热点阅读