当前位置:网站首页>TS function
TS function
2022-07-25 23:17:00 【Intern Sen】
TS function
1. Define and call functions
function fo() {
// Function definition
console.log(" Call function ")
}
fo() // Call function
2. Function with return value
type yes Type of return value ,return The keyword is followed by the result to be returned .
function fo():type{
return value; // The type of the return value needs to be the same as the return value type defined by the function (type) Agreement .
}
3. Functions with parameters
// p1、p2 For the parameter name ,type Is the parameter type
function fo( p1 [:type], p2 [:type]) {
... }
Example : When the parameter is fixed , Incoming parameter Too much or The lack of All will report wrong. .
function add(p1: number, p2: number): number {
return p1 + p2;
}
console.log(add(1)); // error , Parameter missing
console.log(add(1,2)); // correct 3
console.log(add(1,2,3)); // error , Too many parameters
4. Optional parameters
? Question marks identify optional parameters .
function test(p1: string, p2?: string) {
return p2 ? p1 + " " + p2 : p1;
}
let result1 = test("AAA"); // correct
let result2 = test("AAA", "BBB", "CCC"); // error , Too many parameters
let result3 = test("AAA", "BBB"); // correct
// Optional parameters must follow required parameters .
// If you want to p1 It's optional ,p2 It's a must , Then adjust their position , hold p1 Put it in the back .
// If all are optional parameters , It doesn't matter .
5. Default parameters
After setting the default value of the parameter , If the value of this parameter is not passed in when calling , Use default parameters .
function fo(p1 [:type], p2 [:type] = default_value) {
... }
notes : The parameter cannot be set to both optional and default .
6. The remaining parameters
There is a situation , When you don't know how many parameters to pass to the function , You can use the remaining parameters to define .
Residual parameter syntax , Allow us to pass an indefinite number of parameters as an array .
function fo(p1: string, ...ppp: string[]) {
return p1 + " " + ppp.join(" ");
}
let fos = fo("AAA", "BBB", "CCC", "DDD");
// The last named parameter of the function ppp With ... The prefix , Will become an array of remaining parameters .
// Index value from 0( Include 0) To ppp.length( barring ).
7. Anonymous functions
An anonymous function is a function without a function name .
Anonymous functions are declared dynamically when the program is running , Except that there is no function name , Others are the same as standard functions .
// You can assign an anonymous function to a variable , This expression becomes a function expression .
var res = function([arguments]){
... };
7.1 Anonymous functions with no arguments :
var res = function() {
return "hello world";
}
console.log(res())
7.2 Anonymous functions with parameters :
var res = function(p1: number, p2: number) {
return p1*p2;
}
console.log(res(12,2))
7.3 Anonymous function self call
Anonymous functions are used after the function is called () that will do :
(function () {
console.log("Hello!!")
})()
边栏推荐
猜你喜欢

Network Security Learning notes-1 file upload

npm+模块加载机制

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)

赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?

MVVM model

Family relationship calculator wechat applet source code

The fifth article in the series of radar Fundamentals: the function of radar modulation style

Analysis of the influence of ESM direction finding error on positioning error

Notification设置的小图标显示的是小方块
随机推荐
关于优先队列
POI special effects Market Research
理解的英文(言语理解)
Scaffold installation
Week 2: convolutional neural network
How does PHP remove an element from an array based on the key value
Expression of directional signal -- complex exponential signal
Mongodb features, differences with MySQL, and application scenarios
Enterprise level inventory management system of code audit
Computed and watch listening properties
CTS测试方法「建议收藏」
@Autowired注解 required属性
Analysis of the influence of ESM direction finding error on positioning error
EasyExcel实用技巧
asp日期函数(磁盘函数不正确怎么办)
ETL tool (data synchronization) II
Ssh server CBC encryption mode vulnerability (cve-2008-5161)
多模态——Deep Multi-Modal Sets
[paper notes] a meta reinforcement learning algorithm for causal discovery
Learning notes of technical art hundred people plan (1) -- basic rendering pipeline