From C # to Typescript-function
Although there are classes in typescript, JavaScript is function
still there, which is also different from C #.
C # functions do not work out of class, but Typescript function
, like JavaScript, can work alone.
function Type
Functions can have names as well as C #, or anonymous functions, and anonymous functions are written in two ways:
function Checklogin (name:string, pwd:string): boolean{return true;} Let Checklogin = (name:string, pwd:string) = = {return false;} Let Checklogin = function (name:string, pwd:string) {return true;}
Previous article write variable declaration with write variable typelet str: string
, but none of the above actually writes out the type of the function, such as the last one.let checkLogin
Does not indicate the type of return.
It is inconvenient to use a function as a parameter or a return value if it is not explicitly typed, and it is not convenient to refactor without a smart hint.
The return type of the function is actually composed of the parameter + return type, the following code(name: string, pwd: string) => boolean
Is the return type of Checklogin.
Let Checklogin: (name:string, pwd:string) = = Boolean = function (name:string, pwd:string) {return true;}
The parameter names in the return type do not need to match the true parameter names, only the type is consistent.
Of course, most of the cases do not have to write such a complex return type, the previous article has said type inference, typescript will infer the type of the return value according to the context.
function Parameters
The parameters of the typescript are not the same as the parameters of the JavaScript, and the parameters for invoking the JavaScript function can be many or fewer, but the typescript function needs to ensure that the number of incoming parameters is consistent with the definition.
function parameters in C # can have default values, Typescript is supported, and nullable parameters are also supported.
The default value only needs to be written after the parameter=
A value can be, the default parameter can be anywhere, but in front of the required parameters, you want to use the default value, you need to passundefined
。
The nullable parameter is the same as the Nullable property that was previously said, and the parameter name is added after the?
Number, the nullable parameter must be in the last face.
function Checklogin (name:string, pwd:string, Isadmin:boolean = False, email?: string) {Console.info (isadmin);} Checklogin (' brook '); Cannot compile Checklogin (' Brook ', ' 123456 '); Falsechecklogin (' Brook ', ' 123456 ', undefined); Falsechecklogin (' Brook ', ' 123456 ', false); Falsechecklogin (' Brook ', ' 123456 ', true, ' [email protected] '); True
remaining Parameters
The parameters in JavaScript itself are arrays, which can be any number and can be used in the function bodyarguments
To access.
Typescript can also be supported by the remaining parameters, which are similar to the C #param
。
The format of the remaining parameters is...restParam: string[]
。
function Checklogin (name:string, pwd:string, ... others:string[]) {console.info (Others.join ('));} Checklogin (' Brook ', ' 123456 ', ' [email protected] ', ' 12800 '); [Email protected] 12800
This
this
Always pointing to callers in JavaScript, which is often prone to being pits, often requires similar ES6 beforevar self = this
To getthis
Save them.
ES6 and Typescript have made improvements to this point, using the arrow function to create the functionthis
Automatically saved.
Let Permission = {name: ' Brook ', Checklogin:function () {return function () {Console.info (THIS.N Ame = = = ' Brook '); }}};p Ermission.checklogin () (); Error, this is Undefinedlet permission = {name: ' Brook ', Checklogin:function () {return () = { Console.info (this.name = = = ' Brook '); }}};p Ermission.checklogin () (); Passed, because the arrow function was used
But the abovethis
Still have a flaw, get thethis
Isany
Type, which can be inconvenient to refactor,this
Parameters to improve:
this
A parameter is just a definition that is not required to be passed when used.
interface permission{ name: string; checklogin (this: permission): () =>void;} let permission: permission = { name: ' Brook ', checklogin: function (this: permission) { return () => { Console.info (this.name === ' Brook '); //here is not any, but permission } }};p Ermission.checklogin () ();
This also forced to define a good type, play the advantages of typescript strong type.
Generic Functions
Generic functions are supported in the same way as C #, and are similar in style.
function deserialize<t> (content:string): T {}function additem<tkey, tvalue> (Key:tkey, value:tvalue) {}
Generic constraints are also supported, C # is used where T: object
, and typescript is used extends Object
.
function Deserialize<t extends Object> (content:string): T {}
The generic function type is preceded by more than the normal function type <T>
, such as above deserialize
.
Let deserialize: <T> (content:string) = T;
However, if this is a slightly complex parameter, it can be reconstructed with the interface:
function Deserialize<t extends Object> (content:string): T {}interface serializable<t> {(content:string) : T;} function parse<t> (s:serializable<t>) {}parse (deserialize);
From C # to Typescript-function