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() ...
随机推荐
- Hadoop科普文—常见的45个问题解答
1.Hadoop集群可以运行的3个模式? 单机(本地)模式 伪分布式模式 全分布式模式 2. 单机(本地)模式中的注意点? 在单机模式(standalone)中不会存在守护进程,所有东西都运行在一个 ...
- BNU27937——Soft Kitty——————【扩展前缀和】
Soft Kitty Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class n ...
- 获取单个checkbox选中项
1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$("input:[type='checkbox']: ...
- 【分布式消息队列-MQ】
http://www.cnblogs.com/itfly8/p/5155983.html
- 缓存框架EhCache的简单使用
缓存框架EhCache的简单使用: 1.Spring和EhCache框架整合 1.1导入jar包 <dependencies> <dependency> <groupId ...
- flask-login的简单实用
# encoding: utf-8 from flask import Flask, Blueprint from flask_login import (LoginManager, login_re ...
- [转]纯js导出json到excel(支持chrome)
转自:http://blog.csdn.net/educast/article/details/52775559 function JSONToExcelConvertor(JSONData, Fil ...
- 如何解决git fatal: refusing to merge unrelated histories
我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传. 先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法p ...
- js-TextArea的换行符处理
js-txt文本处理 写自己主页项目时所产生的小问题拿出来给大家分享分享,以此共勉. ---DanlV TextArea的换行符处理 TextArea文本转换为Html:写入数据库时使用 js获取了t ...
- 中国gis100强
广州南方测绘仪器有限公司 北京四维图新科技股份有限公司 北京北斗星通导航技术股份有限公司 高德软件有限公司 北京合众思壮科技股份有限公司 中国地图出版社 广州中海达卫星导航技术股份有限公司 正元地理信 ...