Teaching Newbies since 2014

kauress

  • Home
  • Javascript ❤
    • JavaScript
    • Node.js
  • WebDev Tuts
  • screencasts
  • Resources
  • VR & AR
  • Contact
  • Github
  • Twitter
  • YouTube
  • RSS
  • C++
You are here: Home / Javascript ❤ / JavaScript / Setters and Getters in JavaScript – part 1

August 19, 2020 by: Kauress

Setters and Getters in JavaScript – part 1

Accessors: Getters and Setters

This is part 1  of a brief introduction to getters and setters in JavaScript. Getters and Setters are ES5 features. Objects have two types of properties:

  1. Static data property
  2. Accessor property

So far we have come across static data properties in the objects that we have been making. For example:

let blackLivesMatter = {
status: true,
usecase: 'daily'
}

In this object, status and usecase properties are static. So what then are accessor
properties? Accessor properties are functions that will execute on either getting or setting a value.
The two types of accessor properties are:

Getter property: Upon getting a property of an object such as car.color the value of the
property will be generated implicitly by calling a function. The getter works upon reading a
property.
Setter property: When a property is set for example on an object car.color = ‘red’
a function is executed implicitly passing the value of a property, in this case ‘red’ as an
argument to the function. The return value of the function is set to the property. The setter works upon assignment of a property

Playing with MRTK
Classes in JavaScript – a newbie friendly introduction

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 ·Kauress