TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Reflect defineProperty() Method

JavaScript Reflect defineProperty() Method with examples on javascript reflect methods, apply() construct(), defineProperty() method, deleteProperty(), get(), getOwnPropertyDescriptor(), getPrototypeOf(), has(), isExtensible(), ownKeys(), setPrototypeOf() etc.

<< Back to JAVASCRIPT

JavaScript Reflect.defineProperty() Method

The 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.

Exceptions

This 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf