In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to define and use functions in TypeScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to define and use functions in TypeScript".
Definition and use of functions in TypeScript 1. Declare a function to constrain its parameter passing type, and return value type to pass two parameters. No return value const fun1 = (key: string, value: number): void = > {console.log (key, value); / / "Typescript", 100}; fun1 ("Typescript", 100) Optional parameters can be configured for functions in 2.TypeScript. Arguments in functions can not be passed in ES5 or ES6, but must be passed in TS. If you need to set optional parameters, you must set optional parameters as follows: const fun2 = (a: string, bounded: number) = > {/ / parameter followed by a? The representative can pass parameters or not pass parameters console.log (a); / / typescript} fun2 ("typescript") Note: the configuration optional parameter must be configured to the last parameter, otherwise ts will report an error (although compilation can pass but is not recommended) the function of 3.TypeScript sets the default value / / sets the default value and passes in arguments, which is consistent with ES6 const fun3 = (a: number, b: string = "ECMAScript"): void = > {console.log (a) / / 20 console.log (b); / / typescript}; fun3 (20, "typescript"); / set the default value and no arguments are passed. The default value of B is true const fun4 = (a: number, b: boolean = true): void = > {console.log (a); / / 60 console.log (b); / / true}; fun4 (60) The remaining parameters of the 4.TypeScript function / / receive multiple parameters and put them in a container, which is the same as the rest... in ES6 The three-point operator is the same as const fun5 = (... result: number []): void = > {/ / receives arguments with the variable result and indicates the data type let sum: number = 0; for (let index = 0; index)
< result.length; index++) { sum += result[index];}; console.log(sum);//150};fun5(10, 20, 30, 40, 50);//注意接收多个实参的变量必须放在最后一个,否则会报错 //接收参数,与变量名一一对应const fun6 = (first: string, ...result: string[]): void =>{console.log (first); / / string console.log (result); / / ["number", "boolean", "function", "true"]} fun6 ("string", "number", "boolean", "function", "true") Function overloading in 5.TypeScript / / overloading of methods in java: overloading refers to two or more functions with the same name, but their arguments are different, so function overloading occurs. Overloading in / / typescript: try out multiple functions by providing multiple function type definitions for the same function. / / function overloading TS in TS cannot write curly braces for compatibility with ES5 and ES6 / / A constraint on argument types function dataFn (a: string, b: number): void function dataFn (a: number, b: string): void function dataFn (a: number, b: number): void function dataFn (a: any) B: any): void {/ / determines the type of argument passed in and executes its function body if (typeof (a) = "string") {console.log ("This is string")} if it matches a function If (typeof (a) = = "number" & & typeof (b) = "number") {console.log ("this is number");}; if (typeof (a) = "number") {console.log (a, b) / 20typescript} else {}} dataFn (10,20); dataFn (20, "typescript"); the basic form of the arrow function in 6.TypeScript: let func = num:number = > num; / / only one formal parameter can be'= "followed by the formal parameter name, and constrains its type let func = () = > num / / if there are more than one parameter, write the parameter in () in'= "and constrain its type let sum = (num1, num2): number= > num1 + num2 / / if there is only one execution statement, / / write the execution statement directly after'= > ", and specify its return type. If more than one statement must be written {}, write the code in {}, reassign the return value, and type notes: the this object in the function body is the context in which it is defined, if the arrow function is global. Also point to window, it is recommended to nest another layer of functions outside the arrow function so that the this inside the control can not be used as a constructor, that is, you cannot use the new command, or an error will be thrown. You cannot use an arguments object, which does not exist in the body of the function. If you want to, you can use the rest parameter instead. Const fun8 = (a: number, b: number): void = > {console.log (a, b)} fun8 (10,20) Thank you for your reading, the above is the content of "how to define and use functions in TypeScript". After the study of this article, I believe you have a deeper understanding of how to define and use functions in TypeScript. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.