当前位置:网站首页>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)
}
}
边栏推荐
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- ~71 abbreviation attribute of font
- LeetCode 1560. The sector with the most passes on the circular track
- Error: case label `15 'not within a switch statement
- ~86m rabbit practice
- Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
- [graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
- ~85 transition
- One hundred questions of image processing (1-10)
- Shell_ 01_ data processing
猜你喜欢
One hundred questions of image processing (1-10)
~83 form introduction
Chapter 7__ consumer_ offsets topic
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
[unsolved]7-14 calculation diagram
was unable to send heartbeat
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
随机推荐
「博士毕业一年,我拿下 ACL Best Paper」
Cmake error: could not create named generator visual studio 16 2019 solution
One hundred questions of image processing (1-10)
Shell_ 03_ environment variable
I'm "fixing movies" in ByteDance
Spark's RDD (elastic distributed data set) returns a large result set
One hundred questions of image processing (11-20)
Solr word segmentation analysis
字节跳动技术新人培训全记录:校招萌新成长指南
Monomer application concept
Chapter 1 overview of MapReduce
The 116 students spent three days reproducing the ByteDance internal real technology project
LeetCode 1561. The maximum number of coins you can get
第7章 __consumer_offsets topic
Chapter 7__ consumer_ offsets topic
LeetCode 1020. Number of enclaves
was unable to send heartbeat
After the subscript is used to assign a value to the string type, the cout output variable is empty.
Educational Codeforces Round 122 (Rated for Div. 2)
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021