0

I want to create a closed object in javascript, that can be edited only using Object.defineProperty and not to be edited it in the normal way...

The goal is, I am creating a lib, where users can read an object called dictionary, but they can edit it too! is there any way to have an object that can be read by users, and edited by me ?

2
  • 2
    In JS you cannot protect anything in runtime. Everything can be monkey-patched. Commented Mar 7, 2017 at 22:14
  • @zerkms I have added an answer please check it and tell me what's wrong ! Commented Mar 7, 2017 at 22:35

3 Answers 3

0

It is just not possible to protect any object parts.

See also: How to Create Protected Object Properties in JavaScript

Sign up to request clarification or add additional context in comments.

Comments

0

You can provide some basic protection using Object.defineProperty like this:

var o = { a: 5 };

o._protected = {};
o._protected.a = o.a;
Object.defineProperty(o, 'a', {
  get: function() { return this._protected.a; },
  set: function(value) {
   if (value > 0 && value < 5) {
     this._protected.a = value;
   }
  configurable: false
});

This will constrain changes to property a in this object so they will go through the get (read) / set (update). Of course, in this case the _protected object can be manipulated but it does require the user to consciously 'hack' it. Attempts to directly change property a would be under your control.

In this example, an attempt to set o.a = 6 would result in no change to o.a (of course, you could set it to the maximum allowed value in your set function if that were preferable).

You can prevent changes to o.a by not providing a set function.

This can be handy for ensuring properties only get 'valid' values and I've often used it that way.

Comments

0

I found it! please tell me what's the wrong with this solution:

   var protected = {}

   Object.defineProperty(this,
     'setter', {
       value: function(name , value) {
         protected[name] = value
       },  
       writable: false,                                               
     })  

   Object.defineProperty(this,
     'getter', {
       value: function(name , value) {
         return JSON.parse(JSON.stringify(protected))
       },  
       writable: false,
     })  

  Object.freeze(this.setter)
  Object.freeze(this.getter)

7 Comments

@zerkms check this answer!
@edi check this answer!
This is simply putting some protection on a property called 'getter' and 'setter'. I don't see how this helps you.
it's like I can do whatever I want to the object protected, and the users of the lib can't even edit it... they only can read it... or read a copy of it...
"at least the beginners will not know" --- as usually, the protection cost should not be higher than the cost of the stuff being protected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.