- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use JavaScript Object.defineProperty?
If you want to define a new or modify a property on an object, then use the Object.defineProperty in JavaScript. Use the property like the following −
Object.defineProperty(obj, prop, descriptor)
The following are the parameters −
- obj – Property is defined on this object.
- prop – Name of the property
- descriptor − The descriptor for the property
Example
You can try to run the following code to learn how to implement Object.defineProperty in JavaScript −
<!DOCTYPE html> <html> <body> <script> const obj = {}; Object.defineProperty(obj, 'prop', { value: 20, writable: false }); obj.prop = 10; document.write(obj.prop); </script> </body> </html>
Advertisements