解释 TypeScript 中的箭头函数语法
如果你使用过其他编程语言,比如 Python,你可能听说过 lambda 函数。箭头函数类似于 lambda 函数,它提供了一种更简洁的方式来定义 TypeScript 中的函数。
我们可以通过使用胖箭头并将其存储在变量中来创建不带“function”关键字的函数。之后,我们可以在需要时使用该变量来调用函数。
此外,箭头函数没有身份,因为它时匿名函数。因此,我们可以使用存储它的变量来识别它。
语法
用户可以按照以下语法在 TypeScript 中创建箭头函数。
var variable = (param1: type, ...other params): return_type => {
// code for the function
};
在上面的语法中,用户可以看到我们将箭头函数存储在变量中,并使用胖箭头来创建箭头函数。此外,用户还可以观察我们如何向箭头函数传递参数并定义其返回类型。
示例
在下面的示例中,我们创建了箭头函数并将其存储在 showMessage 变量中。我们在箭头函数中打印了消息以向用户显示。
// creating the anonymous arrow function
var showMessage = () => {
console.log("Your welcome on the TutorialsPoint!");
};
// calling the arrow function.
showMessage();
编译后,它将生成以下 JavaScript 代码:
// creating the anonymous arrow function
var showMessage = function () {
console.log("Your welcome on the TutorialsPoint!");
};
// calling the arrow function.
showMessage();
示例
在这个示例中,我们将箭头函数存储在 mergeString 变量中。我们在箭头函数中传递了三个字符串类型的参数。在箭头函数内部,我们将从参数中获取的字符串合并并打印出来。
我们通过使用 mergeString 变量并传递三个参数来调用箭头函数。
// creating the anonymous arrow function
var mergeString = (str1: string, str2: string, str3: string) => {
// merging the string
let finalString: string = str1 + str2 + str3;
console.log("The merged string is " + finalString);
};
// calling the arrow function.
mergeString("Hi", "Users", "!");
编译后,它将生成以下 JavaScript 代码:
// creating the anonymous arrow function
var mergeString = function (str1, str2, str3) {
// merging the string
var finalString = str1 + str2 + str3;
console.log("The merged string is " + finalString);
};
// calling the arrow function.
mergeString("Hi", "Users", "!");
示例
在这个示例中,我们还为箭头函数定义了返回类型。箭头函数接受两个数字类型的参数,并在将参数相乘后返回数字值。
// defining the test arrow function
// param1 is of type number, and param2 is also of type number
// return type is the number
var test = (param1: number, param2: number): number => {
console.log("The value of the param1 is " + param1);
console.log("The value of the param2 is " + param2);
return param1 * param2;
};
// calling the test function and printing its return value
let res: number = test(10, 20);
console.log("The value of the multiplication is " + res);
编译后,它将生成以下 JavaScript 代码:
// defining the test arrow function
// param1 is of type number, and param2 is also of type number
// return type is the number
var test = function (param1, param2) {
console.log("The value of the param1 is " + param1);
console.log("The value of the param2 is " + param2);
return param1 * param2;
};
// calling the test function and printing its return value
var res = test(10, 20);
console.log("The value of the multiplication is " + res);
示例
在下面的示例中,我们创建了 employee 类,其中包含 get_name() 属性。我们使用箭头函数初始化了 get_name() 属性,该函数返回员工的姓名。
之后,我们创建了 employee 类的对象,并通过获取对象作为引用来调用 get_namme() 方法。
class employee {
// defining the get_name arrow function
get_name = (): string => {
let emp_name: string = "Shubham";
return "The employee name is " + emp_name;
};
}
// creating the object of the employee class
let emp_object = new employee();
// calling the get_name() function of employee class
console.log(emp_object.get_name());
编译后,它将生成以下 JavaScript 代码:
var employee = /** @class */ (function () {
function employee() {
// defining the get_name arrow function
this.get_name = function () {
var emp_name = "Shubham";
return "The employee name is " + emp_name;
};
}
return employee;
}());
// creating the object of the employee class
var emp_object = new employee();
// calling the get_name() function of employee class
console.log(emp_object.get_name());
在本教程中,用户学习了如何在 TypeScript 中使用箭头函数以及示例。在本教程中,我们采用了包含参数和返回类型的不同箭头函数示例。此外,我们还学习了如何使用箭头语法来定义特定类的成员方法。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP