
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Accessor property and its attributes in JavaScript
The accessor property helps us in implementing getter and setter functions in JavaScript.They execute a function on getting or setting a value.
There are four attributes of an accessor property −
- get − It gets called when a property is read. It doesn’t’ take any arguments/
- set − It gets called when a property is set. It takes only one argument.
- enumerable − Allows the object to be iterable when set to true.
- configurable − When set to false it will not allow to delete the property or change its values.
Following is the code for accessor property and its attributes −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>Accessor property and its attributes</h1> <div class="result"></div> <br /> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to use set and get to modify and display person object properties</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let person = { name: "Rohan", age: 22, occupation: "Student", get fullname() { return this.name; }, set job(occupation) { this.occupation = occupation; }, }; BtnEle.addEventListener("click", () => { resEle.innerHTML = "Name = " + person.fullname + " : Occupation = " + person.occupation; resEle.innerHTML += "<br>After modifying occupation property <br>"; person.occupation = "Developer"; resEle.innerHTML += "New Occupation = " + person.occupation; }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Attributes and its types in Data Analytics
- HTML DOM attributes Property
- Explain attributes and the different types of attributes in DBMS?
- How to find the href and target attributes in a link in JavaScript?
- Accessing Attributes and Methods in C#
- Accessing Attributes and Methods in Python
- How to identify the elements by partially comparing to its attributes in Selenium?
- Attributes and usage of element in HTML5
- Attributes and usage of element in HTML5
- attributes in C++
- ArrayBuffer.byteLength Property in JavaScript
- DataView.byteLength property in JavaScript
- DataView.byteOffset property in JavaScript
- DataView.buffer property in JavaScript
- Map.size property in JavaScript

Advertisements