38

Possible Duplicate:
Can Read-Only Properties be Implemented in Pure JavaScript?

I have an Object that I only want to be defined when constructed. How can I prevent the Object reference from being changed?

Community
  • 1
  • 1
qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • http://stackoverflow.com/questions/366047/can-read-only-properties-be-implemented-in-pure-javascript – spender Mar 11 '10 at 20:32

2 Answers2

73

In the current widely available implementation, ECMAScript 3 there is no support for real immutability.

UPDATE: Nowadays, the ECMAScript 5 standard is widely supported. It adds the Object.seal and Object.freeze methods.

The Object.seal method will prevent property additions, still allowing the user to write to or edit the existing properties.

The Object.freeze method will completely lock an object. Objects will stay exactly as they were when you freeze them. Once an object is frozen, it cannot be unfrozen.

More info:

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • If you create a getter function that returns Object.freeze(theObject), then when using the getter it's not possible to change the values of the original object. – Shahar Apr 27 '17 at 07:40
  • Will Object.freeze freeze all properties recursively or just the objects own (top level) properties? –  Dec 14 '18 at 07:25
  • 2
    @MrCholo, `Object.freeze` will work only in the current object level, setting the own properties not configurable and not writable and preventing further extensions (addition of new properties). You can check the internals of freezing in the following section of the spec: https://cms.gt/P7NKq – Christian C. Salvadó Dec 14 '18 at 18:17
  • Both [Object.seal](https://caniuse.com/#feat=mdn-javascript_builtins_object_seal) and [Object.freeze](https://caniuse.com/#feat=mdn-javascript_builtins_object_freeze) are now well supported. – Dave F Apr 17 '20 at 21:42
18

EDIT: This was writtent 10 years ago. I don't consider this to be an up to date answer.

Realistically... by not overwriting it. You could always control access by wrapping it in an object that only offers GetObj with no SetObj, but of course, the wrapper is equally liable to overwriting, as are its "private" member properties that would be "hidden" via the GetObj method.

Actually, question is a dupe:

Can Read-Only Properties be Implemented in Pure JavaScript?

EDIT:

After reading http://javascript.crockford.com/private.html , it is possible to use closure to create variable references that are truely inaccessible from the outside world. For instance:

function objectHider(obj)
{
    this.getObject=function(){return obj;}
}

var someData={apples:5,oranges:4}

var hider=new objectHider(someData);

//... hider.getObject()

where the reference to obj in objectHider cannot be modified after object creation.

I'm trying to think of a practical use for this.

spender
  • 117,338
  • 33
  • 229
  • 351
  • 9
    Actually `hider.getObject` simply returns a reference to the `someData` object, and it *can* be modified, e.g.: `hider.getObject().apples = 0;` then `hider.getObject();` will return the same modified object, since `hider.getObject() == someData`... – Christian C. Salvadó Mar 11 '10 at 21:51
  • Sure, but it's the same object. I only claimed the reference to it was unchangeable, not its properties. Now, if someData also implemented hider tech...? – spender Mar 11 '10 at 22:07
  • 2
    You'd need to create a deep copy of the object for it to work. jQuery's extend can be used to do this: `function (obj) { return $.extend({}, obj); }` – crush Apr 02 '15 at 21:25