0

Im trying to seal an object property .

My question is ,here i have given Object.seal(personObject),this particular object is sealed and does not allow to configure or make any extensions in this object,but as i did not mention on personObject_2 it does allow to extend or configure

How can i make it on prototype .I mean like any class of type person should have/respect this seal.Can we achieve such behaviour

"use strict";
var personModule=(function (module) {
   var  person=function (fname,lname) {
       Object.defineProperty(this,'firstName',{
          get:function () {
            return fname;
          }
          ,set:function (newValue) {
            fname=newValue;
         },
         configurable:true
       });

       Object.defineProperty(this,'lastName',{
         get:function () {
           return lname;
         }
         ,set:function (newValue) {
           lname=newValue;
         },
         configurable:true
       });

       Object.defineProperty(this,'fullName',{
         get:function () {
           return fname+lname;
         },
         configurable:true
       });

     }
     module.person=person;
     return module;
})(personModule || {});

var personObject=new personModule.person( "Raju","Rani");
console.log(personObject.fullName);
Object.seal(personObject);
//delete personObject.firstName;-->It throws error here




var personObject2=new personModule.person( "Shiva","Kumar");

delete personObject2.firstName;

console.log(personObject2.firstName);

Thanks

Srisa
  • 269
  • 1
  • 3
  • 9

2 Answers2

1

Here is Proxy version in case you do not prefer adding Object.seal on constructor

"use strict";
var personModule=(function (module) {
   var  person=function (fname,lname) {
   Object.defineProperty(this,'firstName',{
      get:function () {
        return fname;
      }
      ,set:function (newValue) {
        fname=newValue;
     },
     configurable:true
   });

   Object.defineProperty(this,'lastName',{
     get:function () {
       return lname;
     }
     ,set:function (newValue) {
       lname=newValue;
     },
     configurable:true
   });

   Object.defineProperty(this,'fullName',{
     get:function () {
       return fname+lname;
     },
     configurable:true
   });

 }
 module.person=new Proxy(person, {
     construct(target, args){
         args.unshift(null);
         let ctor = target.bind.apply(target, args);
         let result = new ctor();
         Object.seal(result);
         return result;
     }
 });
 return module;
})(personModule || {});

var personObject=new personModule.person( "Raju","Rani");
console.log(personObject.fullName);
Object.seal(personObject);
//delete personObject.firstName;-->It throws error here




var personObject2=new personModule.person( "Shiva","Kumar");

delete personObject2.firstName;

console.log(personObject2.firstName);
Tolgahan Albayrak
  • 3,118
  • 1
  • 25
  • 28
  • Sure, Person function is hooked with a Proxy instance. when you call "new" on module.person it triggers the Proxy handler's construct function. The target parameter refers to original "person" function and args parameter refers to the arguments passed on "new" call. target.bind.apply creates a bound function with the passed parameters. Then I create instance from that new ctor. And seal the instance and return – Tolgahan Albayrak Nov 26 '16 at 07:47
  • Thanks for the explanation – Srisa Nov 26 '16 at 07:52
0

Did you tried - immutable-js

var personObject = new personModule.person("Raju", "Rani");

var sealed = Immutable.Map(personObject);
Mohan Dere
  • 4,497
  • 1
  • 25
  • 21