es7装饰器

news/2025/2/6 18:18:06/

最近终于上手了node服务,这样一来前端的技术栈又拓宽了许多。这次的项目是公司内部项目用的是nestjs,头一次接触到nestjs最深刻的印象就是打来给你用到了es7的装饰器。在项目开始的时候就预料到要写博客了,知识点很多那就先从装饰器开始吧。

装饰器在其他的语言上已经被广泛的使用了如java,py等。js es7的装饰器可以分为四种分别是类装饰器、方法装饰器、变量装饰器、参数装饰器。装饰器本身也相当于是封装了一个逻辑方法,在使用的时候通过@标识符直接调用。

目前浏览器和node对装饰器的支持还不是很好需要安装相应的babel。
我自己的测试项目的话是用vite,因此还涉及到了ts,我的配置是如下:

tsconfig.json

{"compilerOptions": {"experimentalDecorators": true}
}

vite.config.ts 

import typescript from "@rollup/plugin-typescript";
export default defineConfig({plugins: [typescript({include: ["src/page/nestjs/**/*"],exclude: ["node_modules/**/*"],}),react({babel: {plugins: [["@babel/plugin-proposal-decorators", { legacy: true }],["@babel/plugin-proposal-class-properties", { loose: true }],],},}),],
});

example 

const decoratorTest = () => {function classDecorator(classObj: any) {console.log("classDecorator", classObj);}function functionDecorator(target: User, name: string, descriptor: any) {console.log("functionDecorator", target, name, descriptor);}function filedDecorator(filed: string) {return function (target: User, name: string, descriptor: any) {console.log("filedDecorator", target, name, descriptor, filed);};}function valueDecorator(target: User, propertyKey: string) {console.log("valueDecorator", target, propertyKey);}function paramsDecorator(target: any,methodName: string,paramsIndex: number) {console.log("paramsDecorator", target, methodName, paramsIndex);}@classDecoratorclass User {@valueDecoratorpublic name: string = "234234";constructor() {}@filedDecorator("234234234")@functionDecoratorgetName() {return "111";}testParams(@paramsDecorator params: string) {console.log(params);}}let user = new User();console.log(user.getName());user.testParams("234234");
};

前端的话装饰器的使用暂时还不是很多,目前的前端有使用到的场景也就是mobx。
对于装饰器的话个人更把装饰器看成是一种另类的封装。不过这里也得到了一定的启发就是函数柯里化的应用。例如上面例子中的functionDecorator装饰器,普通装饰器是不能直接传递参数的,通过包裹一层函数的方式然后返回一个函数的方式就可以实现装饰器的传参。


http://www.ppmy.cn/news/401849.html

相关文章

ES7+向量检索实现方法

文章目录 一、前言二、实现1.创建索引2.插入数据3.简单检索三、复杂检索语句补充一、前言 es7之后的版本可以使用向量相似度检索,向量的最高维度为2048,es提供的四种相似度检索方法名如下: 余弦函数向量内积曼哈顿距离欧几里得距离cosineSimilaritydotProductl1norml2norm二…

es6迁移到es7

目录 1 软件部署 1.1 概述 1.2 环境准备 1.3 软件介绍 1.4 安装部署 1.4.1 部署Elasticsearch 1.4.2 部署Kibana 1.4.3 部署Logstash 1.4.4 部署Filebeat 1.5 配置说明 1.5.1 全国通用 1.5.2 地区特殊说明 1.6 添加新服务 1.6.1 部署说明 1.6.2 修改配置 1.6.3…

datax导入es7

问题描述:datax最新脚本在es5.x进行测试。导入es7时,想分词,但是还存了text格式,怀疑不是指定mapping创建的索引,是es自动创建的索引。 解决方法: 1、下载源代码。 2、修改 ESWriter.genMappings // rootM…

es7 时间说明

文章目录 ES时间基本说明ES时间查询或者聚合函数查询 - range聚合 - dateRange聚合 - dateHistogram ES时间基本说明 😀 ES字段类型支持date(时间),值可以是毫秒数,表现为new Date().getTime(),或者已经格…

【JS】之ES7

ES7 1.Array.prototype.includes() includes()作用,是查找一个值在不在数组里,若是存在则返回true,不存在返回false. 1.基本用法: [a, b, c].includes(a) // true [a, b, c].includes(d) // false 2.接收俩个参数:要搜索的值和搜索的开始索引…

Es7 的基础使用

一.基础数据说明: shop 表示数据库 _doc 表示数据表 8.0版本废除 创建索引和map //id 字段自增id //good_sn 商品SKU //good_name 商品名称 //good_introduction 商品简介 //good_descript 商品详情 二.常用请求 1.创建数据库为 good 的索引以及map 2.创建…

ES7

ES7 1.Array.prototype.includes() includes()作用,是查找一个值在不在数组里,若是存在则返回true,不存在返回false. 1.基本用法: [a, b, c].includes(a) // true [a, b, c].includes(d) // false2.接收俩个参数:要搜索的值和搜索的开始索引 …

js深度拷贝对象-ES7

ES7深度拷贝对象 Object.getOwnPropertyDescriptors 该函数返回一个对象所有的属性,甚至包括get/set函数。ES2017加入这个函数的主要动机在于方便将一个对象深度拷贝给另一个对象,同时可以将getter/setter拷贝。 const obj {name: joy,getName(){retu…