Comparing Reflect and Object methods - JavaScript 编辑
The Reflect
object, introduced in ES2015, is a built-in object that provides methods to interface with JavaScript objects. Some of the static functions that exist on Reflect
also correspond to methods available on Object
, which predates ES2015. Although some of the methods appear to be similar in their behavior, there are often subtle differences between them.
The table below details the differences between the methods available on the Object
and Reflect
APIs. Please note that if a method does not exist in an API, it is marked as N/A.
Method Name | Object | Reflect |
---|---|---|
defineProperty() | Object.defineProperty() returns the object that was passed to the function. Returns a TypeError if the property was not successfully defined on the object. | Reflect.defineProperty() returns true if the property was defined on the object and false if it was not. |
defineProperties() | Object.defineProperties() returns the objects that were passed to the function. Returns a TypeError if any properties were not successfully defined on the object. | N/A |
set() | N/A | Reflect.set() returns true if the property was set successfully on the object and false if it was not. Throws a TypeError if the target was not an Object . |
get() | N/A | Reflect.get() returns the value of the property. Throws a TypeError if the target was not an Object . |
deleteProperty() | N/A | Reflect.deleteProperty() returns true if the property was deleted from the object and false if it was not. |
getOwnPropertyDescriptor() | Object.getOwnPropertyDescriptor() returns a property descriptor of the given property if it exists on the object argument passed in, and returns undefined if it does not exist. However, if an object is not passed in as the first argument, it will be coerced into an object. | Reflect.getOwnPropertyDescriptor() returns a property descriptor of the given property if it exists on the object. Returns undefined if it does not exist, and a TypeError if anything other than an object (a primitive) is passed in as the first argument. |
getOwnPropertyDescriptors() | Object.getOwnPropertyDescriptors() returns an object containing a property descriptor of each passed-in object. Returns an empty object if the passed-in object has no owned property descriptors. | N/A |
getPrototypeOf() | Object.getPrototypeOf() returns the prototype of the given object. Returns null if there are no inherited properties. Throws a TypeError for non-objects in ES5, but coerces non-objects in ES2015. | Reflect.getPrototypeOf() returns the prototype of the given object. Returns null if there are no inherited properties, and throws a TypeError for non-objects. |
setPrototypeOf() | Object.setPrototypeOf() returns the object itself if its prototype was set successfully. Throws a TypeError if the prototype being set was anything other than an Object or null , or if the prototype for the object being modified is non-extensible. | Reflect.setPrototypeOf() returns true if the prototype was successfully set on the object and false if it wasn't (including if the prototype is non-extensible). Throws a TypeError if the target passed in was not an Object , or if the prototype being set was anything other than an Object or null . |
isExtensible() | Object.isExtensible() returns true if the object is extensible, and false if it is not. Throws a TypeError in ES5 if the first argument is not an object (a primitive). In ES2015, it will be coerced into a non-extensible, ordinary object and will return false . |
|
preventExtensions() |
| Reflect.preventExtensions() returns true if the object has been made non-extensible, and false if it has not. Throws a TypeError if the argument is not an object (a primitive). |
keys() | Object.keys() returns an Array of strings that map to the target object's own (enumerable) property keys. Throws a TypeError in ES5 if the target is not an object, but coerces non-object targets into objects in ES2015. | N/A |
ownKeys() | N/A | Reflect.ownKeys() returns an Array of property names that map to the target object's own property keys. Throws a TypeError if the target is not an Object . |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论