Typescript函数
编程都是需要函数的,因为有了函数就能复用很多东西了。不仅仅能够复用代码,还能保持代码的简洁性和提高编程的逻辑性。
在原生的JavaScript中,函数的定义有三种,
function foo() {} // 直接声明
var foo = function() {} // 将函数赋值给变量
var Foo = {
a : function() {
}
} // 对象函数
那么在typescript当中,又是怎么样的呢?
1、第一种
function foo():string {
return 'hahaha'
}
var result = foo() // hahaha
这是普通的函数声明,直接返回hahaha。
2、第二种,形参
function foo(a:number):string {
return '这个数字是' + a
}
var result = foo(18) // 这个数字是18
在foo():string,后面的变量告诉该函数返回的是什么值,如果是string则返回的是string类型,如果是:number则返回的是number类型,如果不写,则默认为void类型。
在函数当中,在大括号里写的变量代表的是形参。
正如上诉代码
function foo(a:number):string () {
}
// a:number 代表的是形参
var result = foo(18) // 18代表的是实参,实际传入的参数
凡是有一定的JavaScript基础我们都知道,函数体写了几个形参,就必须传入几个参数,多一个少一个都不行,但是,在typescript中,可以选择性的传实参,这点是比较灵活。
有可选参数的函数,采用?标注
function person(age:number, name?:string) {
let a:string = ''
a = "find" + age + 'years old'
if(name != undefined) {
a = a + '' +name
}
return a + 'person'
}
var result = person(18); // find 18 years old
var result1 = person(18, 'does');
// find 18 does years old
console.log(result);
console.log(result1);
由上诉的例子可以知道,name作为可选参数,是可传可不传的。体现typescript的灵活性。
返回指定类型的函数
同样的,我们也可以给函数增加一个默认的返回参数。
同样复用上面的代码。
function person(age:number, name?:string):string {
let a:string = ''
a = "find" + age + 'years old'
if(name != undefined) {
a = a + '' +name
}
return a + 'person'
}
var result = person(18); // find 18 years old
var result1 = person(18, 'does');
// find 18 does years old
console.log(result);
console.log(result1);
在大括号后面增加:string,默认返回的是string类型的参数
不限参数个数的函数
简单的理解就是,出去逛街的时候,你可以会买一个商品,也可能会买多个商品。所以此时的函数是不确定性的。比如写一个shopping函数
function shopping(...type:string[]):string {
let a:string = "我买到了"
for(let i = 0; i < type.length; i++) {
a = a + type[i]
if(i<type.length) {
a = a + ','
}
}
a = a + "商品"
return a
}
// 传参数的时候就可以多个传值了
var result:string = shopping("包","衣服","裤子")
console.log(result)
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 函数-函数类型
//指定参数类型 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() ...
随机推荐
- ZT A2DP协议笔记
A2DP协议笔记 (2013-07-30 10:07:54) 转载▼ 标签: a2dp bluetooth src sink 分类: Bluetooth 1.概述 A2DP(Advanced ...
- IOS Charles(代理服务器软件,可以用来拦截网络请求)
什么是Charles Charles是一款代理服务器软件,可以用来拦截网络请求 利用Charles能得知大部分公司app的数据来源和数据格式 下载地址:http://www.charlesproxy. ...
- Maven编译Java程序配置
Hive 需要在工程里添加的Jar包: hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar $HIVE_HOME/lib/hive-exe ...
- select poll epoll Linux高并发网络编程模型
0 发展历程 同步阻塞迭代模型-->多进程并发模型-->多线程并发模型-->select-->poll-->epoll-->... 1 同步阻塞迭代模型 bind( ...
- 好用的css库
实现元素各种抖动效果:https://elrumordelaluz.github.io/csshake/
- Windows7下安装配置PostgreSQL10
PostgreSQL安装: 一.windows7下安装过程首先上PostgreSQL官方网站的下载页面https://www.postgresql.org/download/windows/,下载本软 ...
- LVS.md
LVS 概述 简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,官方站点.现在LVS已经是 Linux标准内核的一部分, ...
- DTCoreText 、WKWebView 、UIWebView的比较
DTCoreText .WKWebView .UIWebView的比较 HTML->View 数据解析: WebCore:排版引擎核心,WebCore包含主要以下模块:Loader, Parse ...
- 【YY的GCD】
设 \[f(n)=\sum_{i=1}^N\sum_{j=1}^M[(i,j)=n]\] 我们的答案显然是 \[ans=\sum_{p\in prime}f(p)\] 设 \[F(n)=\sum_{i ...
- P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...