Search code examples
javascriptjavascript-objectsdefineproperty

How to prevent object properties to not be extended in javascript


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


Solution

  • 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);