Use Lecture TypeScript ---- TypeScript function

TypeScript function

Is a set of functions to perform a task with the statement.

You can put the code into different functions. How to divide the code into separate functions is up to you to decide, but logically divided usually carried out each function to perform a specific task based on.

Function declaration tells the compiler function name, return type and parameter. Function definition provides the actual body of the function.

Function definition

Function block is wrapped in braces, the previously used Keywords function:

The syntax is as follows:

FUNCTION_NAME function () 
{ 
    // execute code 
}

call function

Function only by calling it the code within the function can be performed.

The syntax is as follows:

function_name()

Examples

Test function () {    // function definition 
    the console.log ( " call function " ) 
} 
Test ()               // call the function

Function return value

Sometimes we want to execute the function will return the results to the local call it.

It can be achieved by using a return statement.

When using the return statement, the function stops execution and returns the specified value.

The syntax is as follows:

function function_name():return_type { 
    // 语句
    return value; 
}
  • return_type a type of return value.

  • After the return keyword followed by the results to be returned.

  • A function can have only one return statement.

  • The type of the return value consistent with the needs of the return type (return_type) function definition.

Anonymous function:

Anonymous function is not a function function name.

Dynamic anonymous function declarations in the program is running, except there is no function name, the other with the same standard functions.

We can be anonymous function assigned to a variable, this expression becomes a function expression.

Syntax is as follows:

var Fun = function (): Number { 
    
   return  123 ;        

} 
Alert (Fun ()); // call the method

Function with parameters

When you call the function, you can pass it a value, these values ​​are called parameters.

These parameters may be used in the function.

You can send multiple parameters to a function, each parameter using comma separated:

The syntax is as follows:

function getInFo(name: string,age: number):string{
    
     return '${name}---${age}';

}

alert(getInFo('zhangsan',20);
  • name, age is the parameter name

  • string, number for the parameter type.

Optional parameters

In TypeScript function, if we define the parameters, then we must pass these parameters unless these parameters are optional, optional parameters identified by question marks? .

function buildName(firstName: string, lastName?: string) {
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}
 
let result1 = buildName("Bob");  // 正确
let result2 = buildName("Bob", "Adams", "Sr.");  // 错误,参数太多了
let result3 = buildName("Bob", "Adams");  // 正确

可选参数必须跟在必需参数后面。 如果上例我们想让 firstName 是可选的,lastName 必选,那么就要调整它们的位置,把 firstName 放在后面。

如果都是可选参数就没关系。

默认参数

我们也可以设置参数的默认值,这样在调用函数的时候,如果不传入该参数的值,则使用默认参数

function getInFo(name: string,age: number = 20):string{
    
     return '${name}---${age}';

}

alert(getInFo('zhangsan');

实例函数的参数 age 设置了默认值为20,调用该函数时如果未传入参数则使用该默认值

剩余参数

也叫 三点运算符  接受新参传过来的值。

有一种情况,我们不知道要向函数传入多少个参数,这时候我们就可以使用剩余参数来定义。

剩余参数语法允许我们将一个不确定数量的参数作为一个数组传入。

function sun(...result: number[]):number{
   
   var sun = 0;
   for(var i = 0; i < result.length; i++){
      
      sun+=result[i];

    }       
    return sum;

}
alert(sum(1,2,3,4,.....));

可以在sun()中无限的添加参数,不用改方法的定义。

函数重载

重载是方法名字相同,而参数不同,返回类型可以相同也可以不同。

每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。

参数类型不同:

function disp(string):void; 
function disp(number):void;

 

参数数量不同:

function disp(n1:number):void; 
function disp(x:number,y:number):void;

 

参数类型顺序不同:

function disp(n1:number,s1:string):void; 
function disp(s:string,n:number):void;

如果参数类型不同,则参数类型应设置为 any

参数数量不同你可以将不同的参数设置为可选。

实例

function myInFo(name: string): void; 
function myInFo(age: number, name: string): void; 
 
function myInFo(class: any, phone ?: any): void { 

    console.log(class); 
    console.log(phone ); 

} 
myInFo("小明") 
myInFo(1,"晓峰");

 

Lambda 函数

Lambda 函数也称之为箭头函数。

箭头函数表达式的语法比函数表达式更短

var getInFo = function (name: string,age: number):string => {
    
     return '${name}---${age}';

}

alert(getInFo('zhangsan',20);

( ) => { }

 相当于

function myInFo(): void { 

    

} 

无参数时可以设置空括号。

好了,ts的函数就到这里吧,和js有点儿不同。

 

Guess you like

Origin www.cnblogs.com/mqflive81/p/11444287.html