当前位置:网站首页>JS reflect
JS reflect
2022-06-30 04:09:00 【Runqing】
Reflect Is a built-in object , It provides interception JavaScript How to operate , yes ES6 New... For manipulating objects API.
Reflect It's not a function object , So it's not constructable .
Reflect All properties and methods of are static .
significance
- At this stage , Some methods are at the same time Object and Reflect Object , New methods in the future will only be deployed in Reflect On the object .
- Modify some Object method , Make it more standardized . Such as Object.defineProperty(obj, name, desc) When properties cannot be defined , It throws an error , and Reflect.defineProperty(obj, name, desc) Will return false.
- Give Way Object Operations become function behaviors .
- Reflect Object method and Proxy Object's methods correspond one to one , As long as it is Proxy Object method , You can be in Reflect Find the corresponding method on the object .
a key
Method
Reflect Object currently has 13 A static method .
// To call a function , At the same time, you can pass in an array as the call parameter .
Reflect.apply(target, thisArg, args)
// On the constructor new operation , Equivalent to execution new target(...args).
Reflect.construct(target, args)
// Get the value of an attribute on the object , Be similar to target[name]. If this property is not available , Then return to undefined.
Reflect.get(target, name, receiver)
// Functions that assign values to properties . Return to one Boolean, If the update is successful , Then return to true.
Reflect.set(target, name, value, receiver)
// Reflect.defineProperty The method is basically equivalent to Object.defineProperty, Define a new property directly on an object , Or modify the existing properties of an object , The difference is ,Object.defineProperty Return this object . and Reflect.defineProperty Will return a Boolean value .
Reflect.defineProperty(target, name, desc)
// As a function delete The operator , Equivalent to execution delete target[name].
Reflect.deleteProperty(target, name)
// Determine whether an object has an attribute , and in Operator It has exactly the same function .
Reflect.has(target, name)
// Return a containing all its own attributes ( Does not contain inherited properties ) Array of .( Be similar to Object.keys(), But it won't be affected enumerable influence , Object.keys Returns a string array of all enumerable properties ).
Reflect.ownKeys(target)
// Determine whether an object is extensible ( Can I add new properties to it ), Be similar to Object.isExtensible(). Returns a that indicates whether a given object is extensible Boolean .(Object.seal or Object.freeze Methods can mark an object as non extensible .)
Reflect.isExtensible(target)
// Make an object non extensible , That is, you can never add new attributes .
Reflect.preventExtensions(target)
// If the attribute exists in the object , If the specified property exists on the object , Then its property descriptor object (property descriptor), Otherwise return to undefined. Be similar to Object.getOwnPropertyDescriptor().
Reflect.getOwnPropertyDescriptor(target, name)
// Returns the prototype of the specified object . Be similar to Object.getOwnPropertyDescriptor().
Reflect.getPrototypeOf(target)
// Set the object prototype function . Return to one Boolean, If the update is successful , Then return to true. If target No Object , or prototype Neither the object nor null, Throw a TypeError abnormal .
Reflect.setPrototypeOf(target, prototype)
Use
// Reflect.apply
var obj1 = {
};
Reflect.apply(Math.floor, obj1, [1.88]) // 1;
// Reflect.construct
const obj2 = Reflect.construct(Date, [2021, 3, 1])
// Reflect.get
var obj3 = {
x: 1, y: 2 };
Reflect.get(obj3, "x"); // 1
// Reflect.set
var obj4 = {
};
Reflect.set(obj4, "prop", "value"); // true
// Reflect.defineProperty(
const obj5 = {
};
Reflect.defineProperty(obj5, 'property', {
value: 666,
writable: false
}); // true
// Reflect.deleteProperty
var obj6 = {
x: 1, y: 2 };
Reflect.deleteProperty(obj6, "x"); // true
obj; // { y: 2 }
// Reflect.has
const obj7 = {
x: 0}
Reflect.has(obj7, "x"); // true
// Reflect.ownKeys
const obj8 = {
z: 3, y: 2, x: 1}
Reflect.ownKeys(obj8); // [ "z", "y", "x" ]
// Reflect.isExtensible
var obj9 = {
};
Reflect.isExtensible(obj9); // true
// Reflect.preventExtensions
var obj10 = {
};
Reflect.isExtensible(obj10); // true
Reflect.preventExtensions(obj10);
Reflect.isExtensible(obj10); // false
// Reflect.getOwnPropertyDescriptor
const obj11 = {
x: "hello"}
Reflect.getOwnPropertyDescriptor(obj11, "x");
// {value: "hello", writable: true, enumerable: true, configurable: true}
// Reflect.getPrototypeOf
var obj12 = {
};
Reflect.getPrototypeOf(obj12); // Equate to Object.prototype
// Reflect.setPrototypeOf
var obj13 = {
};
Reflect.setPrototypeOf(obj13, null); // true
边栏推荐
- Node red series (28): communication with Siemens PLC based on OPC UA node
- 学校实训要做一个注册页面,要打开数据库把注册页面输入的内容存进数据库但是
- When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?
- Machine learning notes
- SQL append field
- [Thesis reading | deep reading] role2vec:role based graph embeddings
- 2021-11-04
- Everyone, Flink 1.13.6, mysql-cdc2.2.0, the datetime (6) class extracted
- DBT product initial experience
- Project safety and quality
猜你喜欢

El upload upload file (manual upload, automatic upload, upload progress)

Grasp grpc communication framework in simple terms

Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview

Day 9 script and resource management

GIS related data

Day 11 script and game AI

el-upload上传文件(手动上传,自动上传,上传进度)

Huawei cloud native - data development and datafactory

(03). Net Maui actual combat basic control

I get n offers in two months. I don't have any difficult interviewers here
随机推荐
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
【论文阅读|深读】DANE:Deep Attributed Network Embedding
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code
[note] May 23, 2022 MySQL
487-3279(POJ1002)
thinkphp5实现导入功能
dotnet-exec 0.5.0 released
节点CODE相同会导致数据重复
Unity when entering a string in the editor, escape the input of characters
Node-RED系列(二八):基于OPC UA节点与西门子PLC进行通讯
UML diagrams and list collections
How to analyze and solve the problem of easycvr kernel port error through process startup?
[punch in - Blue Bridge Cup] day 2 --- format output format, ASCII
你清楚AI、数据库与计算机体系
NER中BiLSTM-CRF解读score_sentence
Cloud native -- websocket of Web real-time communication technology
I get n offers in two months. I don't have any difficult interviewers here
If you encounter problems when using spark for the first time, please ask for help
Knowledge - how to build rapport in sales with 3 simple skills
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里