js ES6 Reflect All In One
js ES6 Reflect All In One
Reflect is a built-in object that provides methods for interceptable JavaScript operations.
The methods are the same as those of proxy handlers.
Reflect is not a function object, so it's not constructible.
Reflect 是一个内置对象,它为可拦截的 JavaScript 操作提供方法。
这些方法与代理处理程序的方法相同。
Reflect 不是一个函数对象,所以它是不可构造的。
Unlike most global objects, Reflect is not a constructor.
You cannot use it with a new operator or invoke the Reflect object as a function.
All properties and methods of Reflect are static (just like the Math object).
与大多数全局对象不同,Reflect 不是构造函数。
您不能将它与 new 运算符一起使用,也不能将 Reflect 对象作为函数调用。
Reflect 的所有属性和方法都是静态的(就像 Math 对象一样)。
Reflect.apply()
Reflect.construct()
Reflect.defineProperty()
Reflect.deleteProperty()
Reflect.get()
Reflect.getOwnPropertyDescriptor()
Reflect.getPrototypeOf()
Reflect.has()
Reflect.isExtensible()
Reflect.ownKeys()
Reflect.preventExtensions()
Reflect.set()
Reflect.setPrototypeOf()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect
反射
数据拦截
依赖注入
依赖收集
Reflect vs Object
demo
const duck = {
name: 'Maurice',
color: 'white',
greeting: function() {
console.log(`Quaaaack! My name is ${this.name}`);
}
}
Reflect.has(duck, 'color');
// true
Reflect.has(duck, 'haircut');
// false
Reflect.ownKeys(duck);
// [ "name", "color", "greeting" ]
Reflect.set(duck, 'eyes', 'black');
// returns "true" if successful
// "duck" now contains the property "eyes: 'black'"
Decorator
@decorator
// @experimentalDecorators
function first() {
console.log("first(): factory evaluated");
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log("first(): called");
};
}
function second() {
console.log("second(): factory evaluated");
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
console.log("second(): called");
};
}
class ExampleClass {
@first()
@second()
method() {
console.log('666');
}
}
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
// @experimentalDecorators
function first() {
console.log("first(): factory evaluated");
return function (target, propertyKey, descriptor) {
console.log("first(): called");
};
}
function second() {
console.log("second(): factory evaluated");
return function (target, propertyKey, descriptor) {
console.log("second(): called");
};
}
class ExampleClass {
method() {
console.log('666');
}
}
__decorate([
first(),
second(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], ExampleClass.prototype, "method", null);
https://www.typescriptlang.org/docs/handbook/decorators.html
refs
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15882932.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-02-13 读书笔记《这里是中国》
2021-02-13 2021 春节贺岁档电影
2020-02-13 ES6 class & call init method
2020-02-13 svg & stroke & style & class
2020-02-13 三角函数
2020-02-13 svg 矩阵转换
2019-02-13 智能货柜 & 技术原理 (动态视觉识别 + 重力感应) All In One