TypeScript----函数
function add(x:number,y:number):number{
return x+y;
}
let myAdd=function(x:number,y:number):number{
return x+y;
}
let myAdd1:(x:number,y:number)=>number=function(x:number,y:number):number{
return x+y;
}
二、函数参数形式
(1)可选参数,参数名旁使用 ? ,可选参数必须跟在必须参数后面 。
function buildName(firstName: string, lastName?: string) {
if (lastName)
return firstName + " " + lastName;
else
return firstName;
}
let result1 = buildName("Bob"); // works correctly now
let result2 = buildName("Bob", "Adams", "Sr."); // error, too many parameters
let result3 = buildName("Bob", "Adams"); // ah, just right
(2) 默认参数,=,可以传入undefined值
function buildName1(firstName: string, lastName = "Smith") {
return firstName + " " + lastName;
}
let result4 = buildName1("Bob"); // works correctly now, returns "Bob Smith"
let result5 = buildName1("Bob", undefined); // still works, also returns "Bob Smith"
let result6 = buildName1("Bob", "Adams", "Sr."); // error, too many parameters
let result7 = buildName1("Bob", "Adams"); // ah, just right
(3)剩余参数
function buildName2(firstName: string, ...restOfName: string[]) {
return firstName + " " + restOfName.join(" ");
}
let employeeName = buildName2("Joseph", "Samuel", "Lucas", "MacKinzie");
三、函数中的this,箭头函数
四、函数重载
参数不同,函数名相同
TypeScript----函数的更多相关文章
- TypeScript 函数 (五)
传递给一个函数的参数个数必须与函数期望的参数个数一致. 参数类别: 必须参数 可选参数 :可选参数必须在参数后面. 默认参数 :当用户没有传递这个参数或传递的值是undefined时. 它们叫做有默认 ...
- TypeScript入门三:TypeScript函数类型
TypeScript函数类型 TypeScript函数的参数 TypeScript函数的this与箭头函数 TypeScript函数重载 一.TypeScript函数类型 在上一篇博客中已经对声明Ty ...
- typeScript函数篇
typeScript的函数是在es6的函数特性的基础上加了一些后端的概念:泛型.参数类型声明.返回值类型声明.重载.装饰器等.其他的一些特性:箭头函数.生成器.async-await.promise等 ...
- typescript函数(笔记非干货)
函数类型 Function Type 为函数定义类型 Define types for functions 我们可以给每个参数添加类型之后再为函数本身添加返回值类型. TypeScript能够根据返回 ...
- Typescript函数
编程都是需要函数的,因为有了函数就能复用很多东西了.不仅仅能够复用代码,还能保持代码的简洁性和提高编程的逻辑性. 在原生的JavaScript中,函数的定义有三种, function foo() {} ...
- TypeScript 函数-函数类型
//指定参数类型 function add(x:number,y:number){ console.log("x:"+x); // reutrn(x+y); } //指定函数类型 ...
- typescript函数类型接口
/* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...
- typescript 函数(定义、参数、重载)
代码: // 本节内容 // 1.函数的定义 // 2.参数(可选参数/默认参数/剩余参数) // 3.方法的重载 // js // function add(x,y){ // return x+y ...
- TypeScript 函数-重载
function attr(name:string):string; function attr(age:number):string; function attr(nameorage:any):an ...
- TypeScript 函数-Lambads 和 this 关键字的使用
let people = { name:["a","b","c","d"], /* getName:function() ...
随机推荐
- 【Linux】Linux C socket 编程之UDP
发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...
- 深入理解JavaScript系列(结局篇)
介绍 最近几个月忙得实在是不可开交,终于把<深入理解JavaScript系列>的最后两篇“补全”了,所谓的全是不准确的,因为很多内容都没有写呢,比如高性能.Ajax安全.DOM详解.Jav ...
- 很多个java面试题
1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...
- 冒泡排序——Python实现
一.排序思想 排序思想参见:https://www.cnblogs.com/luomeng/p/10161794.html 二.python实现 def bubble_sort(nums): &quo ...
- hdu 3466 Proud Merchants 01背包变形
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- 第3章 css属性color的RGBA值
颜色之RGBA RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法: color:rgba ...
- centos7编译安装git最新版
假如系统已经安装了git,先删除. 如果是通过yum安装的,直接在终端使用以下指令删除: yum remove git 如果是通过源码编译安装的,参考以下文章: Linux ./configure & ...
- 表单校验常用原生js库
1.字符串去除左右空格继承形式// 除去左右空格String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, & ...
- jQuery阻止向上冒泡事件
//阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...
- 002服务提供者Eureka
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka.Spring Boot Starter Actuator依赖和Spring Cloud依赖管理 <dependenc ...