前言
努力学习,希望每天进步一点。
A.方法装饰器
function age (constructor:function){
constructor.prototype.age=18
}
function method(target:any,propertyKey:string){
console.log(target)
console.log('prop'+propertyKey)
}
@age
class Hello{
name:string;
age:string;
constructor(){
console.log('hello')
this.name='casey'
}
}
@method
hello(){
return 'hello method'
}
@method
static shello(){
return 'static method'
}
B.可传参的方法装饰器
function log(config){
return function (target:any,name:string):void{
console.log(target,name)
let old=target[name]
target[name]=function(){
if(config=='top'||config=='all'){
console.log('hello')
}
old()
if(config==='bottom'||config==='all'){
console.log('world')
}
}
}
}
class Person{
@log('top')
say():void{
console.log("我是原来的say方法")
}
@log('all')
run():void{
console.log('我是原来的run方法')
}
}
let demo=new Person()
demo.say()
demo.run()
C.属性装饰器
function Value(value:string){
return function(target:any,propertyName:string){
target[propertyName]=value
}
}
class Hello{
@Value('博文') name:string
}
D.装饰器加载顺序
function One() {
return function(target) {
console.log('I am class One')
}
}
function Two() {
return function(target, methodName: string) {
console.log('I am method-class Two')
}
}
function Three() {
return function(target, paramsName: string) {
console.log('I am params-class Three')
}
}
function Four() {
return function(target, paramsName: string) {
console.log('I am params-class Four')
}
}
function Five() {
return function(target, protoName: string) {
console.log('I am proto-class Five')
}
}
@One()
class Hello {
@Five()
hello: string
@Two()
haha( @Three() p1: string, @Four() p2: string ) {
console.log(p1, p2)
}
}
E.对象接口
interface a { a: number, b: boolean, c: string }
let obj: a = {
a: 123,
b: true,
c: 'aa'
}
console.log(obj)
F.类接口
interface UserInterface {
name: string;
age: number;
say(): void
}
class User implements UserInterface {
constructor(public name: string, public age: number) {}
say(): void {
console.log('脱贫脱单不脱发!')
}
}
let user = new User('xiaolun', 18)
console.log(user)
user.say()
G.接口
function printFn(obj: {label: string}) {
console.log(obj.label)
}
let myObj = {size: 1, label: 'hello world'}
printFn(myObj)
interface ObjValue {
label: string
}
function printFun(obj: ObjValue) {
console.log(obj.label)
}
let myObj2 = {size: 1, label: 'hello world22222'}
printFun(myObj2)
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): {color: string; area: number} {
let newSquare = {color: 'white', area: 100}
if(config.color) {
newSquare.color = config.color
}
if(config.width) {
newSquare.area = config.width
}
return newSquare
}
let myA = createSquare({color: 'red', width: 1000})
console.log(myA)
interface Point {
readonly x: number;
readonly y: number;
}
let p1: Point = {x: 10, y: 20}
console.log(p1);
let a: number[] = [1, 2, 3]
let ro: ReadonlyArray<number> = a
interface SearchFn {
(source: string, sub: string): boolean
}
let mySearch: SearchFn
mySearch = function(src: string, sub: string) {
let res = src.search(sub)
return res > -1
}