Unsettable & Unwritable properties are still mutable
<p>I am trying to create a property within a constructor function which is immutable except through a prototype function. I am trying to go off MDN documentation of this: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties</a>. But there does not seem to be a way to make a property completely immutable. Consider a simple example:</p>

<pre><code>function Test(){
    Object.defineProperties(this,{
        elems : { value : [] }
    })
}
Test.prototype.addElem = function(newElem){
    if (this.elems.indexOf(newElem) == -1){
        this.elems.push(newElem);
    }
};
</code></pre>

<p>which works fine in most cases (not assignable):</p>

<pre><code>&gt;a = new Test()
Object { , 1 more… }
&gt;a.elems
Array [  ]
&gt;a.elems = 10
10
&gt;a.elems
Array [  ]
</code></pre>

<p>Unfortunately, it is still mutable. Consider:</p>

<pre><code>&gt;a.elems.push(10)
1
&gt;a.elems
Array [ 10 ]
</code></pre>

<p>I am sure they are other functions (array or object methods?) that will change the value of a non-writeable &amp; non-settable property. Push was just the one I ran into. Is there a way to accomplish this? I know that one possible solution is :</p>

<pre><code>function Test(){
    var elems = [];
    this.addElem = function(newElem){
        if (elems.indexOf(newElem) == -1){
            elems.push(newElem);
        }
    }
}
</code></pre>

<p>But I have read this is memory-inefficient especially when there are many instances of the "class". Also, what I am working on may have many methods like this, so I am even more worried about memory considerations.</p>

<p>Any ideas? I am not super knowledgeable about all the intricacies of JS prototyping.</p>