【发布时间】:2014-05-26 20:25:36
【问题描述】:
假设您有一个服务器端 Javascript 应用程序,并且您从客户端收到以下数据:
[
{
"userId": "0001",
"Details": [
{
"Name": "John",
"Surname": "Doe",
"bankAccount": "123"
}
]
}
]
将这些数据封装到 Javascript 对象中是否是一种好习惯?例如,
var Person = (function () {
var bankAccount = "";
function Person(userId, name, surname, bankAccount) {
this.userId = userId;
this.name = name;
this.surname = surname;
this.getBankAccount = function() {
return this.bankAccount;
};
this.setBankAccount = function(ba) {
this.bankAccount = ba;
};
}
return Person;
})();
这样我可以控制涉及用户敏感数据的操作:
var p = new Person(1, "John", "doe", "123");
p.name
=> 'John'
p.bankAccount
p.getBankAccount()
p.setBankAccount(123)
p.getBankAccount()
=> 123
但是,如果我需要将这些数据保存到像 MongoDB 这样的 NoSQL 数据库中,我需要将所有内容序列化回 JSON,这不是很奇怪吗?我想知道您对这种做法有何看法。
【问题讨论】:
标签: javascript json mongodb