C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Reflect.defineProperty() MethodThe static method Reflect.defineProperty() allows the precise addition to or modification of a property on an object. The Reflect.defineProperty() method returns a Boolean value which indicates that whether or not the property was successfully defined. Syntax
Reflect.defineProperty(target, propertyKey, attributes) Parameter
target: It is the target object which defines the property. propertyKey: It is the name of the property to be defined or modified. Attributes: It is the attributes for the property being defined or modified. Return value:
This method returns a Boolean value which indicates that whether or not the property was successfully defined. ExceptionsThis exception will throw a TypeError if the target is not an Object. Example 1
const u = {};
const result = Reflect.defineProperty(u, "p",
{ value : 6,});
console.log( u );
Output: Object { }
Example 2
const u = {};
const su = Reflect.defineProperty(u, "p",
{ value : 3,
writable: true,
//write
// enumerable: true,
//configurable: true
}
);
console.log( u );
console.log( su);
Output: Object { }
true
Example 3
const object1 = {};
const object2 = {};
(Reflect.defineProperty(object2, 'property2', {value: 12}))
if (Reflect.defineProperty(object1, 'property1', {value: 42})) {
console.log('property1 created!');
} else {
console.log('problem creating property1');
}
console.log(object1.property1);
console.log(object2.property2);
Output: "property1 created!" 42 12
Next TopicJavaScript Reflect
|