Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed Jan 17, 2020
1 parent 10a6fd2 commit 9192188
Show file tree
Hide file tree
Showing 29 changed files with 6,109 additions and 2 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,4 @@ Desktop.ini
*/log
LOG_PATH_IS_UNDEFINED/

/webpack
/ts
/nodeJs
4 changes: 4 additions & 0 deletions ts/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"javascript.implicitProjectConfig.checkJs": true,
"javascript.implicitProjectConfig.experimentalDecorators": true
}
28 changes: 28 additions & 0 deletions ts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// function greet(person: string) {
// return "hello "+ person
// }
// console.log(greet([0,1,3]))
// interface Person {
// firstName: string;
// lastName: string;
// }
// function greeter(person: Person) {
// return 'hello, '+person.firstName+ ' '+person.lastName
// }
// let user = {firstName: 'yang',lastName: 'benyang'}
// console.log(greeter(user))

var Student = /** @class */ (function () {
function Student(firstName, middleName, lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.fullName = firstName + ' ' + middleName + ' ' + lastName;
}
return Student;
}());
function greeter(person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
var user = new Student("Jane", "M.", "User");
console.log(user);
38 changes: 38 additions & 0 deletions ts/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// function greet(person: string) {
// return "hello "+ person
// }

// console.log(greet([0,1,3]))

// interface Person {
// firstName: string;
// lastName: string;
// }

// function greeter(person: Person) {
// return 'hello, '+person.firstName+ ' '+person.lastName
// }
// let user = {firstName: 'yang',lastName: 'benyang'}

// console.log(greeter(user))

class Student {
fullName: string;
constructor(public firstName, public middleName, public lastName) {
this.fullName = firstName + ' '+middleName + ' ' +lastName
}
}

interface Person {
firstName: string
lastName: string
}


function greeter(person : Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}

let user = new Student("Jane", "M.", "User");

console.log(user)
Empty file added ts/tsconfig.json
Empty file.
19 changes: 19 additions & 0 deletions ts/任意值类型.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let xx: string = 'string'

xx = 'xx'
// xx = 5 报错,不能改变类型

let aaa: any = 'any'
aaa = 5 //ok

let anyThing: any = 'hello'

console.log(anyThing.myName);
console.log(anyThing.myName.firstName);

//可以认为,声明一个变量为任意值之后,对它的任何操作,返回的内容的类型都是任意值。

let something //变量如果在声明的时候,未指定其类型,那么它会被识别为任意值类型:

something = '122'
something = 11
4 changes: 4 additions & 0 deletions ts/函数的类型.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//函数声明
function sum(x, y) {
return x + y;
}
41 changes: 41 additions & 0 deletions ts/函数的类型.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//函数声明

function sum(x: number, y:number): number {
return x + y
}

//输入多余的(或者少于要求的)参数,是不被允许的:

let mySum = function (x: number, y: number): number {
return x + y;
}

let mySum: (x: number, y: number) => number = function (x: number, y: number): number {
return x + y;
}

//在 TypeScript 的类型定义中,=> 用来表示函数的定义,左边是输入类型,需要用括号括起来,右边是输出类型。

//用接口定义函数的形状

interface SearchFunc {
(source: string, subString: string): boolean;
}

let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
return source.search(subString) !== -1;
}

//可选参数
// 与接口中的可选属性类似,我们用 ? 表示可选的参数
// 需要注意的是,可选参数必须接在必需参数后面。换句话说,可选参数后面不允许再出现必需参数了:
function buildName(firstName: string, lastName?: string) {
if (lastName) {
return firstName + ' ' + lastName;
} else {
return firstName;
}
}
let tomcat = buildName('Tom', 'Cat');
let tom = buildName('Tom');
15 changes: 15 additions & 0 deletions ts/基本类型.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//ts的原属数据类型
//布尔值
var isDone = false;
var tt = new Boolean(1);
// number
var num = 5;
// 字符串
var myName = 'yyyy';
var myAge = 20;
//模版字符串
var tesrString = "my name is " + myName + "\ni am " + myAge + " years old\n";
//空值
function cc() {
console.log('my name is yyy');
}
49 changes: 49 additions & 0 deletions ts/基本类型.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//ts的原属数据类型

//布尔值

let isDone: boolean = false

let tt = new Boolean(1);

// number
let num: number = 5

// 字符串
let myName: string = 'yyyy'
let myAge: number = 20

//模版字符串

let tesrString: string = `my name is ${myName}
i am ${myAge} years old
`

//空值

function cc(): void { //用 void 表示没有任何返回值的函数:
console.log('my name is yyy')
}

//声明一个 void 类型的变量没有什么用,因为你只能将它赋值为 undefined 和 null

let uuu: void = null
let xxx: void = undefined

//null undefined

//在 TypeScript 中,可以使用 null 和 undefined 来定义这两个原始数据类型:

let u: undefined = undefined
let n: null = null


//与 void 的区别是,undefined 和 null 是所有类型的子类型。也就是说 undefined 类型的变量,可以赋值给 number 类型的变量:


let nummm: number = undefined
let nnnnn: number = null
//let nnnn: undefined = 11 报错

let v: void
// let num: number = v 会报错
17 changes: 17 additions & 0 deletions ts/对象的类型--接口.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型。
var tom = {
name: 'tom',
age: 15
};
var ttttt = {
name: 'xxx'
};
var sss = {
name: 'dadad'
};
var tomdsds = {
id: 89757,
name: 'Tom',
gender: 'male'
};
tomdsds.id = 9527;
56 changes: 56 additions & 0 deletions ts/对象的类型--接口.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型。

interface Person2 {
name: string
age: number
}

const tom:Person2 ={
name:'tom',
age: 15
}

// const ttt:Person2 = {
// name: 'xx' //缺失一个属性,多一个属性都会报错
// }

//可选属性

interface Person3 {
name: string
age?: number //加个问号表示可选类型
}

const ttttt:Person3 = {
name:'xxx' ,//少一个属性也不会报错
}

//任意属性

interface Person4 {
name: string
age?: number
[propName:string]: any //一旦定义了任意属性,那么确定属性和可选属性的类型都必须是它的类型的子集
}

let sss:Person4 = {
name: 'dadad'
}

//只读属性

interface Person5 {
readonly id: number;
readonly name: string;
age?: number;
[propName: string]: any;
}

let tomdsds: Person5 = {
id: 89757,
name: 'Tom',
gender: 'male'
};

tomdsds.id = 123 //报错

5 changes: 5 additions & 0 deletions ts/数组的类型.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 定义方式
// 「类型 + 方括号」表示法
var arr = [1, 1, 3, '4']; //Type 'string' is not assignable to type 'Number'.
//必须是number类型
arr.push('8');
41 changes: 41 additions & 0 deletions ts/数组的类型.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 定义方式

// 「类型 + 方括号」表示法

let arr: Number[] = [1, 1, 3, '4'] //Type 'string' is not assignable to type 'Number'.

//必须是number类型

arr.push('8') //Argument of type '"8"' is not assignable to parameter of type 'Number'

// 数组泛型

let arr2: Array<number> = [1, 1, 2, 3, 5]

// 用接口表示数组

interface NumberArray {
[index: Number]: number
}

let arr3: NumberArray = [1,2,3,4]

//NumberArray 表示:只要索引的类型是数字时,那么值的类型必须是数字。

// 类数组

function sum() {
let args: {
[index: number]: number;
length: number;
callee: Function;
} = arguments;
}

function sum() {
let args: IArguments = arguments;
}

//any的应用

let list: any[] = ['1',2, {}]
7 changes: 7 additions & 0 deletions ts/类型tuilun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//let myFavoriteNumber = 'seven';
//myFavoriteNumber = 7; //报错

//上面的代码等价与

//let myFavoriteNumber: string = 'seven';
//myFavoriteNumber = 7;
14 changes: 14 additions & 0 deletions ts/联合类型.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let mmmm: number | string

mmmm = 'xx'
mmmm = 1
//mmm = true 报错

function xxx(): string { //返回值必须为字符串
return 'xxx'
}

function yyy(mmmms:number|string):string {
// return mmmms.length //报错
return mmmms.toString()
}
Binary file added webpack/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions webpack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/first/node_modules
Loading

0 comments on commit 9192188

Please sign in to comment.