typeScript entry - variable type and function parameters

String properties

Automatically split string (can be called a method of string with a template):

When you call a method with a template string, Common string is divided into an array to the variable corresponding to the output will be:

function greeter(template, myname: string, age: number) {
    console.log(template)
    console.log(myname)
    console.log(age)
}
const myname = "Jane";
const age = 20
greeter`my name is ${myname}, age: ${age}`

Output:

Ts files compiled into a top js files, source code is also very interesting, on the global (window) mounted a method, or an array of methods to generate the final return, just add a row attribute, the array can not be added to a enumeration row attribute value of the array itself, and finally returned array:

// 用了单例设计思想
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
function greeter(template, myname, age) {
    console.log(template);
    console.log(myname);
    console.log(age);
}
var myname = "Jane";
var age = 20;
greeter(__makeTemplateObject(["my name is ", ", age: ", ""], ["my name is ", ", age: ", ""]), myname, age);

 

Variable Types

:string

After the defined type may be variable, the variable is not defined, subject to the type of value

let obj: string = '123'
let count = 123 // 说明count类型为数字类型,下次为count赋值别的类型ts中会报错

:any

Any type of value, a variable may be assigned to any type of

let count: any = 123
count = '23'

:number和 :boolean

Numbers and Boolean

let obj: boolean = true
let count: number = 123

Is there a method return values   

: Void no return value

Written in brackets behind, the method does not require any return value

function test(): void{}

: String must return a string type

function test(): string{}

··· ···

Custom Types

Can be custom class, as a variable of type

class Test{
    private myname: any;
    constructor(name) {
        this.myname = name
    }
}
let test: Test = new Test('lxc')

 

Function Arguments

?: Optional

Below the age is an optional parameter that can be passed from time pass, if there are parameters behind the age, age after parameter must be the default value of the parameter

function test(myname:string, age ?:number){}
function test(myname:string, age ?:number, height:number=180){}

tips:

If you use the optional parameter value, optional parameters need to be addressed early in the method body, the usually optional parameter can be used as a boolean value, such as:

Whether vue source, an optional application parameters, in fact, it is the definition of an object's properties can be enumerated, enumerable attribute is optional, if not pass, enumerable is false

export function def (obj: Object, key: string, val: any, enumerable?: boolean) {
  Object.defineProperty(obj, key, {
    value: val,
    enumerable: !!enumerable,
    writable: true,
    configurable: true
  })
}

 

Published 155 original articles · won praise 21 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42778001/article/details/104219234