当前位置:网站首页>TypeScript基本操作
TypeScript基本操作
2022-07-06 09:29:00 【社会你磊哥,命硬不弯腰】
接口
TS接口我的理解就是面向对象编程中的一种规范,定义接口后不用担心函数内部状态数据,它只规定这批类里面必须提供某些方法,满足规定的开发需要这样。
属性类型接口
interface Iperson{
//?代表可选参数
firstname?:string;
lastname:string
}
function showFullName(person:Iperson){
return person.firstname+person.lastname
}
let person = {
firstname:'ren',
lastname:'jialei',
age:8
}
console.log(showFullName(person))
console.log(showFullName({
lastname:'jialei'}))
函数类型接口
interface encrypt{
(key:string,value:string):string
}
var md5:encrypt = function(key:string,keyvalue:string):string{
return key+keyvalue
}
console.log(md5('ren','jialei'));
类类型接口 对类的约束,和抽象类比较相似
interface Animail{
name:string;
eat(foot:string):void
}
//类类型接口也可实现继承
interface Person extends Animail{
work():void
}
//Dog这个类应该实现Animail接口
class Dog implements Animail{
name:string
constructor(name:string){
this.name=name
}
eat(){
console.log(this.name+'吃老鼠')
}
}
//LEIGE这个类应该实现Person这个接口
class LEIGE implements Person{
name:string = 'renjialei'
work(){
console.log(this.name+'喜欢写代码')
}
eat(){
console.log(this.name+'喜欢吃水果')
}
}
let dog = new Dog('小猫');
let leige = new LEIGE();
dog.eat();
leige.eat();
leige.work();
装饰器
装饰器就是一个方法,它可以注入到类,方法,属性参数上扩展他们的功能
类装饰器
类装饰器应用的是构造函数,也就是我们拿到的参数target就是类的构造函数。
function logclass(params:string){
return function(target:any){
//相当于在类的原型上定义方法
target.prototype.eat=()=>{
console.log('磊哥喜欢吃西瓜');
}
}
class HttpClient{
@logUrl('xxxxxx')
url='www.baidu.com'
constructor(){
}
@logMethod('www.baidu.com')
getItem(){
}
setItem(@logArg('xxx') uuid:any){
}
}
var http:any = new HttpClient();
属性装饰器
属性装饰器表达式会在运行时当作函数被调用,分别传入下列2个参数:
第一个参数是对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
第二个参数是成员的名字。
function logUrl(params:string){
return function(target:any,attr:any){
target[attr]=params;
console.log(target,attr);
}
}
方法装饰器
函数接收三个参数,分别是:
第一个参数是对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
成员的名字
成员的属性描述符
function logMethod(params:string){
return function(target:any,attr:any,desc:any){
target.run = ()=>{
// console.log('i am running');
}
}
}
方法参数装饰器
接收三个参数
第一个参数是对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
参数的名字
参数在函数参数列表中的索引
function logArg(params:string){
return function(target:any,attr:any,index:number){
console.log(target)
console.log(attr)
console.log(index)
}
}
边栏推荐
- Ffmpeg command line use
- 字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
- Spark's RDD (elastic distributed data set) returns a large result set
- 图像处理一百题(11-20)
- Audio and video development interview questions
- The 116 students spent three days reproducing the ByteDance internal real technology project
- 字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
- Chapter 7__ consumer_ offsets topic
- @RestController、@Controller
- 【锟斤拷】的故事:谈谈汉字编码和常用字符集
猜你喜欢
字节跳动开源GAN模型压缩框架,算力最高节省97.8%丨ICCV 2021
Chapter 6 rebalance details
7-4 harmonic average
Spark independent cluster dynamic online and offline worker node
我走过最迷的路,是字节跳动程序员的脑回路
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
Simple records of business system migration from Oracle to opengauss database
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
「博士毕业一年,我拿下 ACL Best Paper」
Eureka single machine construction
随机推荐
LeetCode 1558. Get the minimum number of function calls of the target array
LeetCode 1557. The minimum number of points that can reach all points
Simply try the new amp model of deepfacelab (deepfake)
@RestController、@Controller
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
Continue and break jump out of multiple loops
Chapter III principles of MapReduce framework
LeetCode 1560. The sector with the most passes on the circular track
Market trend report, technical innovation and market forecast of double-sided foam tape in China
LeetCode 1566. Repeat the pattern with length m at least k times
Research Report on market supply and demand and strategy of China's tetraacetylethylenediamine (TAED) industry
Detailed explanation of FLV format
Research Report on market supply and demand and strategy of Chinese table lamp industry
~81 long table
第7章 __consumer_offsets topic
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
第5章 NameNode和SecondaryNameNode
「博士毕业一年,我拿下 ACL Best Paper」