0

I have some global object CD and it has set of properties as follows:

window.cd = {
  config:{
    title:"..."
  }
  a:func..., 
  b:56,
  c:..
}

I want user to allow add properties to cd and cd.config but properties cd.a, cd.b, cd.config.title to be sealed (no modification allowed) .

User should not be able to delete window.cd or these properties as well (Other properties he can delete or modify).

I tried with following:

window.cd.a.seal();
window.cd.b.seal();
window.cd.config.title.seal();

but it thrown following error:

window.cd.config.title.seal is not a function

4

1 回答 1

0

使标题不可写的快速示例

"use strict";
// Use strict mode or assignments to nonwritable properties silently fail

window.cd = {
  config:{
    title:'mytitle'
  },
  a:12,
  b:56
}

console.log(cd.config.title)
Object.defineProperty(cd.config,'title',{writable:false});
cd.config.title="Test";  // Fails, throws exception if Strict mode
console.log(cd.config.title)
于 2017-08-11T15:48:03.947 回答