What you're doing in your example is not getters/setters
in JavaScript, you're setting a function on an object that creates a closure over two 'private' members of that object (and you're leaking a reference for the array). You could refer to helper functions as 'getters' and 'setters' as you would in another language, but this could lead to confusion.
Helper functions would look like this:
var Obj = function() {
var myVal = 20;
this.getVal = function() {
return myVal;
};
this.setVal = function(val){
// validation of val
myVal = val;
};
};
There are quite a few different ways to define 'getters and setters', I recommend you check out MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_Getters_and_Setters
This is one of my more favorite ways:
var o = { a:0 }
Object.defineProperties(o, {
"b": { get: function () { return this.a + 1; } },
"c": { set: function (x) { this.a = x / 2; } }
});
o.c = 10 // Runs the setter, which assigns 10 / 2 (5) to the 'a' property
console.log(o.b) // Runs the getter, which yields a + 1 or 6
edit
Remember, a constructor function will either return itself (the object created by the function) or an object returned from the function. You can use that to use the Object.defineProperties
on some object o
, then just return o
at the end of the constructor function.
/edit
References
To answer your question about leaking the internal array reference outside of the helper function, I'd recommend cloning the array. You'll need to also consider that the internals of the of array may also be object references instead of primitives and determine how far you want to lock this 'private' state of your object.
Personally, I use frameworks and clone
functionality. For instance, you can use underscore.js's clone()