Search code examples
javascriptencapsulation

Are named accessor properties only just about getters and setters


An accessor property associates a key value with one or two accessor functions, and a set of Boolean attributes. The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property.

The above is from ecmascript specification 6th edition.

Since everything in javascript are objects, and all javascript properties are named data properties (Ndps) ( to the best of my knowledge ) ... And named accessor properties (Naps) as Getters and Setters are there more to Naps than getters and setters. And the retrieval of a Ndp does it come straight from the underneath js engine like v8 or there more about its implementation on the higher level?

Example code:

var stuff = { 'stuff1': 1, 'stuff2': 2 }    // stuff as an Object

stuff.stuff1 // returns 1

Object.defineProperty(stuff, 'stuff3', {
    // defining stuff3's property attributes
    get: function() { return 'Hey! This is from the getter function'}
    enumerable: true,
    configurable: true
})

stuff.stuff1 // returns but how does it

stuff.stuff3 // returns and i know how because it was defined.

Is there any other implementation of stuff's operation?

Please other reference materials or links should be noted.


Solution

  • There isn't really anything you can do with a property except GET it or SET it, because that's how stuff works (pardon the pun). Accessor functions provide means of doing so, but more importantly they provide you with a means of controlling how this is done. A GET function might require that you're logged in, or have certain access rights, for example. A SET function might prevent you from setting negative values, or only allow logged in users to use it, log the transaction to file etc.