JavaScript 对象私有(private)参数

标签 javascript private javascript-objects

如何创建这样的对象。
对象应该有一个名称,一旦设置名称就无法修改。

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/

相关文章:

c# - 派生类中的抽象方法覆盖,如何使私有(private)

javascript - 如何获取 javascript 中对象数组内的对象数组的长度?

javascript - 3种不同类型的JavaScript对象,有什么区别?

javascript - 将键值对(数组中的值)添加到数组中的特定对象

javascript - jQuery、表格、CSS 和更改 tr 宽度 : what's wrong?

javascript - 使用 AngularJS html5Mode (back4app/Parse-Server) 时如何解析 ExpressJS 路由

javascript - meteor JS : Installing on apache/linux server

javascript - 将 npm 模块包装为 Ember Addons

c++ - 私有(private)数据成员访问

java - 为什么 Play 框架模板可以访问私有(private)字段?