Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 885 Bytes

what_are_javascript_accessors.md

File metadata and controls

23 lines (17 loc) · 885 Bytes

What are JavaScript accessors?

Accessors are methods that allow you to get or set the value of an object's property. In JavaScript, accessors are usually implemented using get and set methods.

Example:

const person = {
  firstName: 'John',
  lastName: 'Doe',
  get fullName() { return this.firstName + ' ' + this.lastName; },
  set fullName(name) { const parts = name.split(' '); this.firstName = parts[0]; this.lastName = parts[1]; }
};
console.log(person.fullName); // 'John Doe'
person.fullName = 'Jane Smith';
console.log(person.firstName); // 'Jane'

Tags: basic, JavaScript, objects

URL: https://www.tiktok.com/@jsmentoring/photo/7459533167528119585