TheDeveloperBlog.com

Home | Contact Us

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

JavaScript Object defineProperty() Method

JavaScript Object defineProperty() Method with example on javascript, object, javascript assign(), create(), defineProperties(), defineProperty(), entries(), freeze(), javascript tutorial, javascript array etc.

<< Back to JAVASCRIPT

JavaScript Object.defineProperty() Method

The Object.defineProperty() method defines a new property directly on an object and returns the object. To change the flags, we can use Object.defineProperty. We cannot change it back, because define property doesn?t work on non-configurable properties.

Syntax:

Object.defineProperty(obj, prop, descriptor)

Parameter

Obj: The Object on which to define the property.

Prop: The name of a property to be defined or modified.

Descriptor: The descriptor for the property being defined or modified.

Return:

This method returns the object that was passed to the function.

Browser Support:

Chrome Yes
Edge Yes
Firefox 1.5
Opera Yes

Example 1

const object1 = {};
Object.defineProperty(object1, 'property1', {
  value: 22, } );
  object1.property1;
    // throws an error in strict mode
console.log(object1.property1);

Output:

22

Example 2

const object1 = {};
Object.defineProperty(object1, 'property1', {
  value: 42,
  value: 52,
  value: 542,
  });
object1.property1 = 177;
// throws an error in strict mode
console.log(object1.property1);

Output:

542

Example 3

const object1 = {};
Object.defineProperty(object1, 'property1', {
  
  value: 2,
  value: 4,
  value: 4+13,
  });
object1.property1 ;
console.log(object1.property1);

Output:

17

Next TopicJavaScript Objects




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