首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Reflect.set

静态方法?Reflect.set()工作方式就像在一个对象上设置一个属性。

语法

代码语言:javascript
复制
Reflect.set(target, propertyKey, value[, receiver])

参数

target设置属性的目标对象。propertyKey设置的属性的名称。value设置的值。receiver如果遇到 setter,this 将提供给目标调用。

返回值

返回一个Boolean值表明是否成功设置属性。

异常

抛出一个TypeError,如果目标不是?Object

描述

Reflect.set?方法允许你在对象上设置属性。它的作用是给属性赋值并且就像?property accessor语法一样,但是它是以函数的方式。?

示例

使用Reflect.set()

代码语言:javascript
复制
// Object
var obj = {};
Reflect.set(obj, 'prop', 'value'); // true
obj.prop; // "value"

// Array
var arr = ['duck', 'duck', 'duck'];
Reflect.set(arr, 2, 'goose'); // true
arr[2]; // "goose"

// It can truncate an array.
Reflect.set(arr, 'length', 1); // true
arr; // ["duck"];

// With just one argument, propertyKey and value are "undefined".
var obj = {};
Reflect.set(obj); // true
Reflect.getOwnPropertyDescriptor(obj, 'undefined');
// { value: undefined, writable: true, enumerable: true, configurable: true }

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Reflect.set' in that specification.

Standard

Initial definition.

ECMAScript Latest Draft (ECMA-262)The definition of 'Reflect.set' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

49

(Yes)

42 (42)

No support

No support

10

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

No support

49

(Yes)

42.0 (42)

No support

No support

10

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com