如何创建这样的对象。
对象应该有一个名称,一旦设置名称就无法修改。
var movies = {
genre:"Horror";
}
movies.genre= "Pop" I dont want to change my genre value
最佳答案
您实际上可以更改对象的属性。
经过一番研究后发现,请根据您的需要进行修改。
var data = { };
Object.defineProperty(data, 'secret', {
value: 42,
writable : false,
enumerable : true,
configurable : false
});
来自@jAndy的回答:
That way, we created a property secret with the value
42
within data, which cannot get modfied nor deleted.The caveat here is, that the genius (like you called it) will also be able to spot this code and just modify that, to again be able to change the content if he wishes to. You just can't create a secured front-end JavaScript in that way. The code will always be available as plain-text for everybody.
来源:How to prevent the changing of a variable value in Javascript
关于JavaScript 对象私有(private)参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23447641/