当前位置:网站首页>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)
}
}
边栏推荐
- SQL快速入门
- Hbuilder x format shortcut key settings
- 我走過最迷的路,是字節跳動程序員的腦回路
- 第7章 __consumer_offsets topic
- ~79 Movie card exercise
- Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- 图像处理一百题(11-20)
- @RequestMapping、@GetMapping
- Basic principles of video compression coding and audio compression coding
猜你喜欢

Solr standalone installation

Chapter 6 rebalance details

~69 other ways to use icon fonts

Record the error reason: terminate called after throwing an instance

LeetCode 1641. Count the number of Lexicographic vowel strings

The QT program compiled on CentOS lacks a MySQL driven solution

「博士毕业一年,我拿下 ACL Best Paper」

图像处理一百题(1-10)

LeetCode 1020. Number of enclaves

Fdog series (VI): use QT to communicate between the client and the client through the server (less information, recommended Collection)
随机推荐
LeetCode 1550. There are three consecutive arrays of odd numbers
TypeScript基本操作
两个礼拜速成软考中级软件设计师经验
Shell_ 06_ Judgment and circulation
Solve the single thread scheduling problem of intel12 generation core CPU (II)
Educational Codeforces Round 122 (Rated for Div. 2)
Redis standalone startup
~71 abbreviation attribute of font
LeetCode 1640. Can I connect to form an array
README. txt
我在字节跳动「修电影」
7-7 ring the stupid bell
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
FLV格式详解
字节跳动技术面试官现身说法:我最想pick什么样的候选人
SQL快速入门
图像处理一百题(1-10)
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
Fdog series (4): use the QT framework to imitate QQ to realize the login interface, interface chapter.
Cartesian tree (modified)