当前位置:网站首页>Thirteen grammar learning of reflect
Thirteen grammar learning of reflect
2022-07-21 04:58:00 【A good programmer】
Reflect Is a built-in object , It provides interception JavaScript How to operate . These methods and
proxy handlers (en-US) The same way .Reflect
It's not a function object , So it's not constructable .
Default call | function |
Reflect.get(target, key, receiver) | Read object properties |
Reflect.set(target, key, value, receiver) | Setting of object properties |
Reflect.deleteProperty(target, key) | Delete properties on objects |
Reflect.ownKeys(target) | Returns an array of attribute keys of the target object itself |
Reflect.getOwnPropertyDescriptor(target, key) | Gets the property descriptor of the given property |
Reflect.defineProperty(target, propKey, propDesc) | Define or modify the properties of an object |
Reflect.preventExtensions(target) | Prevent new properties from being added to objects |
Reflect.getPrototypeOf(target) | Function that gets the prototype of the specified object |
Reflect.isExtensible(target) | Determine whether an object is extensible |
Reflect.setPrototypeOf(target, proto) | A function that sets or changes the prototype of an object |
Reflect.apply(target, object, args) | Operate on a function , Colleagues can pass in an array as the call parameter |
Reflect.construct(target, args) | On the constructor new operation , To create an instance of a class |
Reflect Grammar one :Reflect.has(target, propertyKey)
explain : Determine whether an object has an attribute , and in The function of the operator is exactly the same .
// grammar : Reflect.has(target, propertyKey): Determine whether an object has an attribute ,
and in The function of the operator is exactly the same .
const sum = {
name:" Zhang San ",
age:18
}
console.log(Reflect.has(sum,"age")) //true
console.log(Reflect.has(sum,"hobby")) //false
Reflect Grammar II :Reflect.deleteProperty(object, 'property')
grammar : static state Reflect.deleteProperty() Method allows deleting attributes . It's like As function Of delete Operator .
// grammar : static state Reflect.deleteProperty() Method allows deleting attributes .
It's like As function Of delete Operator .
const object1 = {
property1: 42,
property2: 43,
};
Reflect.deleteProperty(object1, 'property1');
// Return value :Boolean Value indicates whether the attribute was successfully deleted .
console.log(object1); //{property2: 43}
Reflect Grammar three :Reflect.isExtensible()
Determine whether an object is extensible ( Whether new attributes can be added ). With it Object.isExtensible() The method is similar , but
There are some differences .
Reflect Grammar 4 : Reflect.preventExtensions(object1)
Static methods Reflect.preventExtensions(object1) Return to one Boolean Value indicates whether the target object was successfully set
Is not extensible .
/*
Static methods Reflect.isExtensible() Determine whether an object is extensible ( Whether new attributes can be added ).
With it Object.isExtensible() The method is similar , But there are some differences .
*/
const object1 = {};
console.log(Reflect.isExtensible(object1));//true( The return value is true Description is extensible )
/*
Static methods Reflect.preventExtensions(object1) Return to one Boolean Value indicates that the target object is
Whether it is successfully set as non extensible .
*/
Reflect.preventExtensions(object1);
console.log(Reflect.isExtensible(object1));// Print false, Indicates that the object is now not extensible
Reflect Grammar five :
Reflect.defineProperty(target,propertyKey,attributes)
/*
Static methods Reflect.defineProperty() It's basically the same as Object.defineProperty() Method ,
The only difference is going back Boolean value .
grammar :Reflect.defineProperty(target, propertyKey, attributes)
target: Target audience
propertyKey: The name of the property to be defined or modified
attributes: A description of the property to be defined or modified .
Return value :Boolean Value indicates whether the property was successfully defined . If target No Object, Throw a TypeError.
*/
let obj = {}
const a = Reflect.defineProperty(obj,'x',{
// get(){
// return 7
// }
value:7
})
console.log(a) //true
Reflect Grammar 6 :
Reflect.getOwnPropertyDescriptor(target, propertyKey)
Static methods Reflect.getOwnPropertyDescriptor() And Object.getOwnPropertyDescriptor() The method is similar .
If there is... In the object , The property descriptor of the given property is returned . Otherwise return to undefined.
/*
Static methods Reflect.getOwnPropertyDescriptor() And Object.getOwnPropertyDescriptor() The method is similar . If there is... In the object , The property descriptor of the given property is returned . Otherwise return to undefined.
grammar :Reflect.getOwnPropertyDescriptor(target, propertyKey)
Parameters :
target You need to find the target object of the attribute .
propertyKey Get the name of the property of your own property descriptor .
*/
let obj1 = {foo:123}
const sum = Reflect.getOwnPropertyDescriptor(obj1, 'foo')
console.log(sum) //{value: 123, writable: true, enumerable: true, configurable: true}
Reflect Grammar seven :Reflect.getPrototypeOf()
Static methods Reflect.getPrototypeOf() And Object.getPrototypeOf() The method is almost the same .
Return the prototype of the specified object ( It's internal [[Prototype]] The value of the property ).
/*
Static methods Reflect.getPrototypeOf() And Object.getPrototypeOf() The method is almost the same .
Return the prototype of the specified object ( It's internal [[Prototype]] The value of the property ).
*/
const object2 = { property1: 42 };
const proto1 = Reflect.getPrototypeOf(object2);
console.log(proto1); // Print object The prototype of the
const proto12 = Reflect.getPrototypeOf(proto1);
console.log(proto12) //null
Reflect Grammar eight :Reflect.ownKeys()
Static methods Reflect.ownKeys() Returns an array of property keys of the target object itself .
/*
Reflect.ownKeys()
Static methods Reflect.ownKeys() Returns an array of property keys of the target object itself .
*/
// If it's an object
let obj3 = {name:' Zhang San ',age:18,hobby:[' Play a ball ',' Play a game ']}
const a1 = Reflect.ownKeys(obj3)
console.log(a1)//name,age,hobby
Reflect Grammar nine :Reflect.get()
Reflect.get() Methods and from object (target[propertyKey]) Read properties similar to ,
But it operates through a function execution .
/*
Reflect.get() Methods and from object (target[propertyKey]) Read properties similar to ,
But it operates through a function execution .
*/
let obj4 = {x:1,y:2}
const a2 = Reflect.get(obj4,'x')
console.log(a2) //1
Reflect Grammar ten :Reflect.set()
Static methods Reflect.set() It works like setting a property on an object .
/*
Static methods Reflect.set() It works like setting a property on an object .
grammar :
Reflect.set(target, propertyKey, value)
target: Set the target object of the property .
propertyKey: The name of the set property .
value: Add value of attribute
*/
let obj5 = {}
Reflect.set(obj5, "prop", "value");
边栏推荐
- # CF #808 Div.2(A - C)
- 自编码器(Auto-Encoder)
- Ibatis and SQL injection
- SaaS最通俗易懂的解释是什么?看这篇就够了
- C language file operation management (Part 1)
- Popular explanation: the difference between IAAs, PAAS and SaaS
- 如何合并多个工作表或多个工作簿?3种合并方法都在这
- 原生threeJS入门
- Kotlin学习之json数据解析
- Storage of integer and floating point numbers in memory
猜你喜欢
RNA 24. Timer, an online gadget for TCGA based analysis of immune infiltrating cells in SCI articles
Excel中实用的3个数据透视表操作技巧,简单高效!
Send a book at the end of the article | Douban 9.4 points, "Hello, world" originated from this book!
Redis的五种数据类型详解
自编码器(Auto-Encoder)
搞懂这些关键指标,数据分析起码少费一半力
为什么说CRM对于企业来说非常重要?看完值得思考
如何合并多个工作表或多个工作簿?3种合并方法都在这
Deep parsing of custom types
HCIP-8.OSPF的优化和拓展配置
随机推荐
Excel中实用的3个数据透视表操作技巧,简单高效!
IP address forgery in security development
Auto encoder
selenium框架操作stealth.min.js文件隐藏浏览器指纹特征
【翻译】读博士一年后对机器学习工程的思考
QBluetoothSocket
Storage of integer and floating point numbers in memory
FigDraw 15. Omiccircos of SCI article drawing
Threejst objects move at a constant speed
關於XML 編輯工具
Solution summary of apktool back compilation problem
基于STM32设计的动态密码锁
用Excel制作甘特图跟踪项目进度(附绘制教程)
《PolarDB for Postgres SQL 》主要讲了什么?
双向LSTM中文微博情感分类项目
A very simple and beautiful login box
If: the molecular composition of 14+ "smoking" myeloma highlights the evolutionary pathway leading to multiple myeloma
尚医通项目总结
SaaS最通俗易懂的解释是什么?看这篇就够了
DNA 6. Genome variation: complexheatmap