TypeScript基础入门之装饰器(二)

news/2024/7/3 13:54:29 标签: javascript, python

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

转发 TypeScript基础入门之装饰器(二)

装饰器求值

如何应用装饰器应用于类内的各种声明的顺序:

1. 对每个实例成员应用参数装饰器,后跟Method,Accessor或Property Decorators。
2. 对每个静态成员应用参数装饰器,后跟Method,Accessor或Property Decorators。
3. 参数装饰器应用于构造函数。
4. 类装饰器适用于该类。

类装饰器

类装饰器在类声明之前声明。
类装饰器应用于类的构造函数,可用于观察,修改或替换类定义。
类装饰器不能在声明文件中使用,也不能在任何其他环境上下文中使用(例如在声明类上)。

类装饰器的表达式将在运行时作为函数调用,装饰类的构造函数作为其唯一参数。

如果类装饰器返回一个值,它将使用提供的构造函数替换类声明。

>注意: 如果您选择返回新的构造函数,则必须注意维护原始原型。在运行时应用装饰器的逻辑不会为您执行此操作。

以下是应用于Greeter类的类装饰器(@sealed)的示例:

@sealed
class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

我们可以使用以下函数声明定义@sealed装饰器:

function sealed(constructor: Function) {
    Object.seal(constructor);
    Object.seal(constructor.prototype);
}

当执行@sealed时,它将密封构造函数及其原型。

接下来,我们有一个如何覆盖构造函数的示例。

function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) {
    return class extends constructor {
        newProperty = "new property";
        hello = "override";
    }
}

@classDecorator
class Greeter {
    property = "property";
    hello: string;
    constructor(m: string) {
        this.hello = m;
    }
}

console.log(new Greeter("world"));

方法装饰器

方法装饰器在方法声明之前声明。
装饰器应用于方法的属性描述符,可用于观察,修改或替换方法定义。
方法装饰器不能用于声明文件,重载或任何其他环境上下文(例如声明类)中。

方法装饰器的表达式将在运行时作为函数调用,具有以下三个参数:

1. 静态成员的类的构造函数,或实例成员的类的原型。
2. 会员的名字。
3. 会员的属性描述

注意: 如果脚本目标小于ES5,则属性描述符将不确定。

如果方法装饰器返回一个值,它将用作方法的属性描述符。

注意: 如果脚本目标小于ES5,则忽略返回值。

以下是应用于Greeter类上的方法的方法装饰器(@enumerable)的示例:

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }

    @enumerable(false)
    greet() {
        return "Hello, " + this.greeting;
    }
}

我们可以使用以下函数声明定义@enumerable装饰器:

function enumerable(value: boolean) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        descriptor.enumerable = value;
    };
}

这里的@enumerable(false)装饰器是一个装饰工厂。
当调用@enumerable(false)装饰器时,它会修改属性描述符的可枚举属性。

未完待续...

转载于:https://my.oschina.net/zhangdapeng89/blog/2250916


http://www.niftyadmin.cn/n/1494301.html

相关文章

怎样在max系统运行Java,如何在mac系统运行servlet helloworld项目

一、写一个项目1.新建项目假设你已经安装了eclipse,新建一个命名为&#xff1a;servletHelloworld 的web项目2.创建类创建一个命名为&#xff1a; Hello 的类&#xff0c;写上包名&#xff1a;com.servletHelloworld添加代码如下&#xff1a;package com.servletHelloworld;imp…

php定义数据库配置,thinkphp3.2.2 没有定义数据库配置

出现这个问题&#xff0c;温习下tp配置多个数据库return array(//默认数据库‘DB_TYPE‘ > ‘mysql‘, // 数据库类型‘DB_HOST‘ > ‘localhost‘, // 服务器地址‘DB_NAME‘ > ‘thinkphp‘, // 数据库名‘DB_USER‘ > ‘root‘, // 用户名‘DB_PWD‘ > ‘roo…

【基础】RandomAccess

在List集合中&#xff0c;我们经常会用到ArrayList以及LinkedList集合&#xff0c;但是通过查看源码&#xff0c;就会发现ArrayList实现RandomAccess接口&#xff0c;但是RandomAccess接口里面是空的&#xff01;Linked并没有实现RandomAccess接口。 RandomAccess接口是一个标志…

oracle无法跟踪web发送的sql,如何通过跟踪客户端程序发出的sql的方法来优化SQL

简要说来&#xff0c;跟踪一个客户程序发出的SQL主要分成下面几步&#xff1a;1) 识别要跟踪的客户端程序到数据库的连接(后面都用session代替)&#xff0c;主要找出能唯一识别一个session的sid与serial#.2) 设定相应的参数&#xff0c;如打开时间开关(可以知道一个sql执行了多…

oracle的默认编码是,ORACLE 默认编码 GBK -UTF8编码

查看oracle数据库字符集&#xff1a;select userenv(language) from dual;SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startup mountORACLE instance started.Total System Global Area 135337420 bytesFixed Size …

nodemailer + express + h5 拖拽文件上传 实现发送邮件

一、部署 1、部署Express 2、准备一个邮箱并开始SMTP服务 二、服务器端 三、客户端 四、效果&#xff1a; 转载于:https://www.cnblogs.com/cwxwdm/p/10601646.html

下载github指定文件夹下的内容

傻瓜操作 http://downgit.zhoudaxiaa.com/#/home 窒息操作 sudo apt-get install subversiongithubdir #!/usr/bin/env pythonimport sys,osif __name__ __main__:urlsys.argv[1]durlurl.replace(tree/master,trunk)cmdf"svn checkout {durl}"os.system(cmd)./g…

dockerfile 的最佳实践

Dockerfile 编写nginx容器 [rootmast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y install gcc gcc-c make openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl && yum clean all && rm -rf /var/cache/…