How can you make a variable/Object read only in Javascript?

61,525

Solution 1

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.

Solution 2

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:

Share:
61,525
qodeninja
Author by

qodeninja

I write qode mostly for myself... out of curiosity for solving problems, understanding how things work or making (sometimes unnecessarily) complex systems to only simplify them later (once I discover alternative strategies). For whatever reason, I like torturing myself with Regular Expressions, SED, Bash and JavaScript (Node), but have found a growing (painful) love with Python. Having said that, I enjoy scripting languages a lot more than compiled languages, and I've coded in almost all of the major modern ones except Ruby. I'm a secret Turing Machine/Computer Grammars/Regular Expressions nerd, and have written my own mini compilers and toy languages. I'm constantly writing command dispatchers that I later write scripting languages for; it's an addiction. There's plenty room for me to grow and learn still; and I appreciate the wisdom of grey beards and lady wizards even if I don't always follow their sage advice. FOSS is hella cool; cool projects are cool. Find me online if you have ideas. I'm a really bad programmer but I'll write a line or two for the betterization of the peoples. Edit: I recently discoved that VI is really just SED with wings. Still not using VI. Nano or bust.

Updated on August 29, 2020

Comments

  • qodeninja
    qodeninja almost 4 years

    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?

  • Christian C. Salvadó
    Christian C. Salvadó over 14 years
    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...
  • spender
    spender over 14 years
    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...?
  • crush
    crush about 9 years
    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); }
  • Shahar
    Shahar about 7 years
    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.
  • Admin
    Admin over 5 years
    Will Object.freeze freeze all properties recursively or just the objects own (top level) properties?
  • Christian C. Salvadó
    Christian C. Salvadó over 5 years
    @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: cms.gt/P7NKq
  • Dave F
    Dave F about 4 years
    Both Object.seal and Object.freeze are now well supported.