如何学习一门语言
是什么? 与es的关系?
以面向对象的思维编程?
代码格式的流转?
我们在做什么?
// C-D 强数据操作
import { observable, action } from 'mobx'
export default class StoreBase {
service: any
@observable state = ''
@observable list = []
@observable loading = false
@observable totalCount = 0
@observable counter = 0
@observable pageSize = 10
@observable pageIndex = 1
@observable submiting = false
@observable initialFormValue = {}
constructor(service) {
this.service = service
}
@action changeSize(pageSize) {
this.pageSize = pageSize
}
@action async getPageList(options: { pageIndex?: number, pageSize?: number }) {
options.pageIndex = options.pageIndex || this.pageIndex
options.pageSize = options.pageSize || this.pageSize
this.pageIndex = options.pageIndex
this.pageSize = options.pageSize
this.loading = true
const result = await this.service.list(options)
this.loading = false
this.list = result.data
this.totalCount = result.totalCount
}
@action async add(body) {
this.submiting = true
try {
const result = await this.service.add(body)
if (result.statusCode && result.statusCode !== 200) {
throw new Error(result.message)
}
} catch (error) {
throw error
} finally {
this.submiting = false
}
}
@action async edit(id, body) {
this.submiting = true
try {
const result = await this.service.update(id, body)
if (result.statusCode && result.statusCode !== 200) {
throw new Error(result.message)
}
} catch (error) {
throw error
} finally {
this.submiting = false
}
}
@action async remove(id) {
try {
await this.service.delete(id)
this.getPageList({ pageIndex: this.pageIndex })
} catch (error) {
throw error
}
}
@action initializeForm(form) {
this.initialFormValue = form
}
}
我们做的web application是由组件集组装起来的, application的质量取决于 组件的质量
什么是组件的质量? 怎样写出质量好的组件?
ts 有什么? 是如何使得我们能产出高质量组件
类型系统 之 原子类型
// boolean
let judge: boolean = true;
// string
let girl: string = `${family.join('======')}`;
// any
const think: any = {}
think = [];
think = '';
think = 1037;
// void
function select(): void {
return null;
}
// never
function logger(message: string): never {
throw new Error(message);
}
类型系统 之 组合类型
// 数组类型
let family: string[] = ["epuisite", "love", "detail"];
let team: Array<number> = [1, 3, 7];
interface teamArray {
[index: number]: any
}
let t: teamArray = [1, 2, 3, {1: 3}];
// 元组
let structure: [string, Array<string>];
structure = ['why', ['atomic element', 'what']];
structure[2] = 'how';
structure[3] = ['when'];
// function类型
interface Doing {
(type: number, thing: string, when?: string|number): any[]
}
let mydoing: Doing;
function f(type: number = 2, thing?: string, when?: string|number, ...items: any[]) {
return type;
}
function sf(message: number): number;
function sf(message: string): string;
function sf(message: number | string): number | string {
if (message as number) {
return 1;
} else {
return '123';
}
}
// 枚举类型 数字类型的值
const Color = {Red, Green, Blue};
enum Direction{
Up=1,
Down,
Left,
Right
}
// 自定义类型
// 对象类型 - 接口
interface Mother {
readonly id: number,
detail: any[],
learning: string,
love: boolean,
housework?: boolean,
[proName: string]: any
}
let me: Mother = {
id: new Date().getTime(),
detail: ['water', 137, 'cloud', 'grass', 'nose'],
learning: 'a wider world',
love: true,
doing() {
return 137;
}
};
工具 是什么? 怎么用? 为什么用?
泛型
function createArray<T>(length: number, value: T): Array<T> {
let result: T[] = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}
function swap<T = number, U = number>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]];
}
interface lengthwish {
length: number
}
function swap2<T extends lengthwish, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]];
}
interface createArrayFunc {
<T extends lengthwish>(length: number, value: T): Array<T>;
}
let myCreateArray: createArrayFunc;
myCreateArray = function <T extends lengthwish>(length: number, value: T): Array[T] {
let result: T[] = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}
// 联合类型[共有的属性和方法 + 类型推论]
let cooperation: string | number | boolean | null | undefined;
cooperation = 1;
cooperation = null;
// 类型断言
function determine(config: number[] | string ): boolean {
if ((<string>config).length || (<number[]>config).length) {
return true;
} else {
return false
}
}
type Name = string;
type DoFn = () => string;
type cof = Name | Dofn;
function o(message: cof): Name {
return '';
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。