当前位置:网站首页>Typescript basic operations
Typescript basic operations
2022-07-06 16:53:00 【Society, you Lei brother, life is hard, don't bend down】
Interface
TS Interface, as I understand it, is a specification in object-oriented programming , After defining the interface, don't worry about the internal state data of the function , It only stipulates that certain methods must be provided in this batch of classes , Meet the specified development needs like this .
Property type interface
interface Iperson{
//? Represents an optional parameter
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'}))
Function type interface
interface encrypt{
(key:string,value:string):string
}
var md5:encrypt = function(key:string,keyvalue:string):string{
return key+keyvalue
}
console.log(md5('ren','jialei'));
Class type interface Constraints on classes , Similar to abstract classes
interface Animail{
name:string;
eat(foot:string):void
}
// Class type interfaces can also implement inheritance
interface Person extends Animail{
work():void
}
//Dog This class should implement Animail Interface
class Dog implements Animail{
name:string
constructor(name:string){
this.name=name
}
eat(){
console.log(this.name+' Eating rats ')
}
}
//LEIGE This class should implement Person This interface
class LEIGE implements Person{
name:string = 'renjialei'
work(){
console.log(this.name+' Like to write code ')
}
eat(){
console.log(this.name+' I like to eat fruit ')
}
}
let dog = new Dog(' kitten ');
let leige = new LEIGE();
dog.eat();
leige.eat();
leige.work();
Decorator
Decorators are one way , It can be injected into classes , Method , Expand their functions on attribute parameters
Class decorator
Class decorators apply constructors , That is, the parameters we get target Is the constructor of the class .
function logclass(params:string){
return function(target:any){
// It is equivalent to defining methods on the prototype of a class
target.prototype.eat=()=>{
console.log(' Brother Lei likes eating watermelon ');
}
}
class HttpClient{
@logUrl('xxxxxx')
url='www.baidu.com'
constructor(){
}
@logMethod('www.baidu.com')
getItem(){
}
setItem(@logArg('xxx') uuid:any){
}
}
var http:any = new HttpClient();
Attribute decorator
Property decorator expressions are called at run time as functions , Respectively pass in the following 2 Parameters :
The first parameter is the constructor of the class for static members , For instance members are prototype objects of a class .
The second parameter is the name of the member .
function logUrl(params:string){
return function(target:any,attr:any){
target[attr]=params;
console.log(target,attr);
}
}
Method decorator
The function takes three arguments , Namely :
The first parameter is the constructor of the class for static members , For instance members are prototype objects of a class .
Member's name
The property descriptor of the member
function logMethod(params:string){
return function(target:any,attr:any,desc:any){
target.run = ()=>{
// console.log('i am running');
}
}
}
Method parameter decorator
Receive three parameters
The first parameter is the constructor of the class for static members , For instance members are prototype objects of a class .
Parameter name
The index of the parameter in the function parameter list
function logArg(params:string){
return function(target:any,attr:any,index:number){
console.log(target)
console.log(attr)
console.log(index)
}
}
边栏推荐
- The 116 students spent three days reproducing the ByteDance internal real technology project
- Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
- JS encapsulates the method of array inversion -- Feng Hao's blog
- LeetCode 1561. The maximum number of coins you can get
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- LeetCode 1562. Find the latest group of size M
- Simple records of business system migration from Oracle to opengauss database
- Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
- Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
- 我在字节跳动「修电影」
猜你喜欢
Usage of insert() in vector
Chapter 2 shell operation of hfds
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
Shell_ 06_ Judgment and circulation
Hbuilder x format shortcut key settings
Sublime text code formatting operation
Spark independent cluster dynamic online and offline worker node
~86m rabbit practice
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
~79 Movie card exercise
随机推荐
7-5 blessing arrived
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
One hundred questions of image processing (11-20)
我在字节跳动「修电影」
视频压缩编码和音频压缩编码基本原理
7-4 harmonic average
After the subscript is used to assign a value to the string type, the cout output variable is empty.
LeetCode 1984. Minimum difference in student scores
Chapter 2 shell operation of hfds
Spark's RDD (elastic distributed data set) returns a large result set
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
Simple records of business system migration from Oracle to opengauss database
Codeforces Global Round 19
Chapter 1 overview of MapReduce
图像处理一百题(1-10)
~Introduction to form 80
7-7 ring the stupid bell
~78 radial gradient
QT system learning series: 1.2 style sheet sub control lookup