2016-11-26 34 views
0

進出口試圖以密封的對象屬性。如何防止對象屬性不以JavaScript擴展

我的問題是,在這裏我給了Object.seal(personObject),這個特定的對象是封閉的,不允許在這個對象中配置或做任何擴展,但是因爲我沒有提到personObject_2,它允許延長或配置

我怎樣才能使它的原型。我的意思是像任何類類型的人應具備/這方面我們seal.Can實現這樣的行爲

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

感謝

+1

加入Object.seal把'Object.seal(this) '在構造函數中? – 4castle

+0

我甚至嘗試過原型......看起來像是在工作 – Srisa

+0

並且還把Object.freeze(this)? – Anson

回答

1

這裏是代理服務器版本的情況下,你不喜歡在構造函數

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

+0

你能解釋一下這裏發生了什麼...... – Srisa

+0

當然,Person函數被一個Proxy實例所吸引。當你在module.person上調用「new」時,它會觸發代理處理程序的構造函數。目標參數指的是原始的「人」功能,args參數是指在「新」呼叫上傳遞的參數。 target.bind.apply使用傳遞的參數創建一個綁定函數。然後我從那個新的ctor創建實例。並密封實例並返回 –

+0

感謝您的解釋 – Srisa

0

你有沒有嘗試過 - immutable-js

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

var sealed = Immutable.Map(personObject);