当前位置:网站首页>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
边栏推荐
- Postman learning sharing
- Jour 9 Gestion des scripts et des ressources
- 487-3279(POJ1002)
- Integrating viewbinding and viewholder with reflection
- matplotlib. pyplot. Hist parameter introduction
- You know AI, database and computer system
- Do280 private warehouse persistent storage and chapter experiment
- win10系统使用浏览器下载后,内容无故移动或删除
- UML图与List集合
- Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL
猜你喜欢

你清楚AI、数据库与计算机体系

Huawei cloud native - data development and datafactory

绿色新动力,算力“零”负担——JASMINER X4系列火爆热销中
![[note] May 23, 2022 MySQL](/img/a1/dd71610236729e1d25c4c3e903c0e0.png)
[note] May 23, 2022 MySQL

巧用 Bitmap 实现亿级海量数据统计

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

About manipulator on Intelligent Vision Group

(03). Net Maui actual combat basic control

Wang Shuang - assembly language learning summary

【模糊神经网络预测】基于模糊神经网络实现水质预测含Matlab源码
随机推荐
Postman learning sharing
idea灰屏问题
Interface test tool postman
Unity 在編輯器中輸入字符串時,轉義字符的輸入
SQL append field
A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
知识点滴 - 如何用3个简单的技巧在销售中建立融洽的关系
I get n offers in two months. I don't have any difficult interviewers here
Integrating viewbinding and viewholder with reflection
An error occurs when sqlyog imports the database. Please help solve it!
Idea grey screen problem
第九天 脚本與資源管理
[operation] getting started with MySQL on May 23, 2022
【图像融合】基于交叉双边滤波器和加权平均实现多焦点和多光谱图像融合附matlab代码
After the win10 system uses the browser to download, the content is moved or deleted without reason
Titanic(POJ2361)
dbt产品初体验
NER中BiLSTM-CRF解读score_sentence
GIS related data
matplotlib. pyplot. Hist parameter introduction