我有javascript getter setter类
function UserContext() {
var category_id;
var biller_id;
this.get_category_id = function () {
return category_id;
}
this.set_category_id = function (value) {
category_id = value;
}
this.get_biller_id = function () {
return biller_id;
}
this.set_biller_id = function (value) {
biller_id = value;
}
}
我在jquery
点击事件
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
我在c#
中有类似的课程public class CustomerDTO
{
public string category_id { get; set; }
public string biller_id{ get; set; }
}
一个asp:hidden
元素
<asp:HiddenField ID="hdnValue" ClientIDMode="Static" runat="server" />
我想要实现的目标
contextObj
分配给asp:hidden
元素(可能采用json格式)CustomerDTO
序列化对象我试过这个
console.log(JSON.stringify(contextObj));
但它什么都没打印。我希望打印值,以便我可以分配隐藏变量
答案 0 :(得分:6)
你的语法错误。问题是你的变量是私有的,stringify()函数无法访问它们。实际上,如果您尝试直接访问私有变量,则会得到“未定义”。
序列化仅适用于公共成员,这是有道理的,因为私有变量不可访问。
这应该可以作为一个快速入侵,但它会使你的变量公开(只要你使用this.category_id = value),所以它破坏了封装,这是不好的:
function UserContext() {
this.category_id;
this.biller_id;
this.get_category_id = function() {
return this.category_id;
}
this.set_category_id = function(value) {
this.category_id = value;
}
this.get_biller_id = function() {
return this.biller_id;
}
this.set_biller_id = function(value) {
this.biller_id = value;
}
}
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
alert(JSON.stringify(contextObj));
&#13;
function UserContext() {
var category_id;
var biller_id;
this.get_category_id = function() {
return category_id;
}
this.set_category_id = function(value) {
category_id = value;
}
this.get_biller_id = function() {
return biller_id;
}
this.set_biller_id = function(value) {
biller_id = value;
}
this.toJSON = function() {
return {
"category_id": category_id,
"biller_id": biller_id
};
};
}
var contextObj = new UserContext();
contextObj.set_category_id('SOME VALUE');
contextObj.set_biller_id('65');
alert(JSON.stringify(contextObj));
&#13;
因此,您的变量只能通过您的setter和getter访问,并且您还可以使用私有成员序列化您的对象!我称之为双赢局面!
答案 1 :(得分:1)
Javascript序列化程序不像C#那样使用setter和getter。 要转换属性,需要将它们公开并设置到您的类中。
function UserContext() {
this.category_id = null;
this.biller_id = null;
this.get_category_id = function () {
return category_id;
}
this.set_category_id = function (value) {
category_id = value;
}
this.get_biller_id = function () {
return biller_id;
}
this.set_biller_id = function (value) {
biller_id = value;
}
}
然后它应该正确地将属性添加到你的json:
var contextObj = new UserContext();
console.log(JSON.stringify(contextObj ));
答案 2 :(得分:1)
将您的代码更改为:
function UserContext() {
var category_id;
var biller_id;
this.get_category_id = function () {
return this.category_id;
}
this.set_category_id = function (value) {
this.category_id = value;
}
this.get_biller_id = function () {
return this.biller_id;
}
this.set_biller_id = function (value) {
this.biller_id = value;
}
}
不要按照其他建议执行此操作:
this.category_id = "";
this.biller_id = "";
这些意味着私密。保持他们这样。
答案 3 :(得分:0)
I think this way of function definition looks fine
function UserContext() {
var category_id = "";
var biller_id = "";
this.get_category_id = function() {
return this.category_id;
}
this.set_category_id = function(value) {
this.category_id = value;
}
this.get_biller_id = function() {
return this.biller_id;
}
alert(JSON.stringify(contextObj));//now check in alert for out put
this.set_biller_id = function(value) {
this.biller_id = value;
}
}
var contextObj = new UserContext();
contextObj.set_category_id('Delhi');
contextObj.set_biller_id('43');