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 NameObjectReflect
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/AReflect.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/AReflect.get() returns the value of the property. Throws a TypeError if the target was not an Object.
deleteProperty()N/AReflect.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.

Reflect.isExtensible() returns true if the object is extensible, and false if it is not. Throws a TypeError if the first argument is not an object (a primitive).

preventExtensions()

Object.preventExtensions() returns the object that is being made non-extensible. Throws a TypeErrorin ES5 if the argument is not an object (a primitive). In ES2015, treats the argument as a non-extensible, ordinary object and returns the object itself.

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/AReflect.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:129 次

字数:8425

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文