ts basic data type (in)

/ * Define the array * /

var arr: number [] = [1, 2, 3];
var arr1: Array<number> = [1, 2, 3];
var arr2: [string, number] = ['this is string', 1];
/ * Enumerated type * /
enum Status {
  success = 200,
  error = 404
}
let the state: state = Status.success; // 200
let statu2: Status = Status.error; // 404
/*
enum Color {
  blue,
  red,
  yellow
}
let blueIndex:Color = Color.blue; // 0
let redIndex:Color = Color.red; // 1
*/
enum Color {
  blue,
  red = 4,
  yellow
}
let blueIndex: Color = Color.blue;
console.log(blueIndex)
let redIndex: Color = Color.red;
console.log(redIndex) // 4
let yellowIndex: Color = Color.yellow;
console.log(yellowIndex) //5

 

/ * Void * type /

 

function run(): void {
  console.log("I am runing")
}

 

function isNumber(): number {
  return 124;
}

 

/ * Function type * /
// function of the type of es5
function eat() {
  return 'eating';
}
var eat2 = function () {
  return 'eating'
}

 

Type of function // ts
function eat3(): string {
  return 'eating';
}
// define the type of transmission parameters
function eat4(name: string, many: number): string {
  return `I'm full $ {name}`;
}
// default parameters

 

function eat5(name: string, many: number = 4): string {
  return `I'm full $ {name}`;
}
// Three operators
function sum(...result: number[]) {
  let sum;
  for (let index = 0; index < result.length; index++) {
    sum += result[index];
  }
  return sum;
}
sum(1,2,3); // 6
 
 
/ * Objects in es5 inheritance * /
/*
 
function Person() {
  this.name = fasd;
  this.age = 4;
  this.worker = function() {
  the console.log ( 'work');
  }
}

 

function Web() {
  Person.call (this); // posing the law of succession objects (objects that inherit properties and methods can not inherit properties and methods prototype chain)

Guess you like

Origin www.cnblogs.com/1212asa/p/11711253.html