6.2 使用对象 //创建对象 let myData = new Object(); //对象赋值 myData.name = "Adam"; myData.weather = "sunny";
6.2.1 使用对象字面量 let myData = { name: "Adam", weather: "sunny" }
对于要定义的属性用:
号隔开,用,
号分隔不同属性。
6.2.2 将函数用作方法 let myData = { name: "Adam", weather: "sunny", printMessage: function () { console.log("Hello "+ this.name +". "); console.log("Today is "+ this.weather +". "); } } myData.printMessage();
注意:方法中引用对象定义的属性,必须使用this
关键字
6.2.3 定义类 class
关键字用于声明一个类,其后是类名。
class MyClass { constructor (name,weather){ this.name = name; this.weather = weather; } printMessage(){ console.log("Hello "+ this.name +". "); console.log("Today is "+ this.weather +". "); } }
定义类的设置器和读取器属性 constructor (name,weather){ this.name = name; this._weather = weather; } set weather( value ){ this._weather = value; } get weather(){ return `Today is ${weather}`; }
读取器和设置器实现方式:在函数名称前面加上get
或set
关键字
JavaScript类中没有访问控制的概念,约定内部属性的名称统一使用下划线(符号_
)作为前缀。
使用类继承 extends
关键字用于声明将要继承的类(被称为超类或者基类)
super
关键字用于调用超类的构造函数或方法
class MySubClass extends MyClass{ constructor (name ,weather, city){ super(name,weather); this.city = city; } printMessage(){ super.printMessage(); console.log(`You are in ${city}`); } } let myData = new MySubClass("Adam","sunny","London"); myData.printMessage();
6.3 处理JavaScript模块 创建和使用模块 默认情况:JavaScript和Typescript文件中定义的类、函数和变量就只能在该文件中访问。
export
关键字用于在文件之外访问特性。
import
关键字用于声明对模块所提供特性的依赖关系。
创建一个名为NameAndWeather.ts
文件内容
export class Name { constructor(first,second){ this.first = first; this.second = second; } get nameMessage(){ return `Hello ${this.first} ${this.second}`; } } export class WeatherLocation{ constructor(weather,city){ this.weather = weather; this.city = city; } get weatherMessage(){ return `It is ${weather} in ${this.city}`; } }
import {Name,WeatherLocation} from './module/NameAndWeather';
重命名导入 export class Name { get message{ return 'Other Name'; } } import {Name as OtherName} from './module/xxx';
导入模块中所以类型 import * as NameAndWeatherLocation from './module/NameAndWeather'; let name = new NameAndWeatherLocation.Name("Adam","Freeman"); let loc = new NameAndWeatherLocation.WeatherLocation("raining","London");
6.4 有用的Typescript特性 为属性和变量添加类型注解 export class Name { first: string; second: string; constructor(first: string ,second: string ){ this.first = first; this.second = second; } get nameMessage() : string { return `Hello ${this.first} ${this.second}`; } } export class WeatherLocation{ constructor(weather: string,city: string){ this.weather = weather; this.city = city; } get weatherMessage(): string{ return `It is ${weather} in ${this.city}`; } }
指定多个类型或任意类型 Typescript允许指定多个类型,使用字符|
进行分隔。
export class TempConverter { static convertFtoC(temp: number | string){ let value: number = (<number> temp).toPrecision ? <number> temp : parseFloat(<string> temp); return xxx;//代码忽略,此处不做实现 } }
使用<>
字符表示声明一个类型断言,尝试将一个对象转换为指定的类型。还可以使用as
关键字达到同样效果。
export class TempConverter { static convertFtoC(temp: number | string){ let value: number = (temp as number).toPrecision ? temp as number : parseFloat(<string> temp); return xxx;//代码忽略,此处不做实现 } }
使用元组 元组:固定长度的数组,数组的每一项都是指定的类型。
let tuple: [string,string,string]; tuple = ["London","raining","38"];
使用可索引的类型 let cities: {[index:string]: [string,string]} = {}; cities["London"]=["raining","38"];
键:字符串
数据值:元组
只有number
和string
值可以用作可索引类型的键。