2014-09-26 88 views
2

我的JavaScript的getter setter方法类序列化JavaScript对象和反序列化

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 click事件创建该类的对象

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" /> 

我想通过序列化实现

  1. 分配contextObjasp:hidden元素(可以是JSON格式)什么
  2. 在后面的代码得到这个对象desrailize它并分配值的相应的c#类,即CustomerDTO
  3. 因此,我可以通过所有页面访问所有这些值请求(通过在Server.Transfer请求中传递此对象)

要序列化对象我想这

console.log(JSON.stringify(contextObj)); 

但不打印输出。我希望打印值,以便我可以分配给隐藏变量

+0

尝试使用'$ .parseJSON()'http://api.jquery.com/jquery.parsejson/ – 2014-09-26 07:52:28

+1

@ZeeTee为什么? – 2014-09-26 07:53:08

+0

序列化时,函数不用于获取值。 您的getter和setter在JavaScript中需要暴露在_class_中。 尝试用关键字this声明你的变量。 – Cyrbil 2014-09-26 07:53:22

回答

6

您的语法错误。问题是你的变量是私人的,他们不会被stringify()函数访问。事实上,如果你试图直接访问一个私有变量,你会得到'未定义'。

序列化仅适用于公共成员,这是有道理的,因为私有变量不可访问。

这应该(只要你使用this.category_id =值)的工作作为一个快速黑客攻击,但它使你的变量公开,所以它破坏了封装这是不好:

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));
更好的方法是保持category_id和biller_id真正私有,并且仍然能够序列化你的对象。你CON做到这一点通过实现你的对象的toJSON()方法,并指定明确要序列什么:
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));

其结果是,你的变量,只有通过你的getter和setter方法访问,你也可以使用您的私人成员序列化您的对象!我会称这是一个双赢的局面!

+1

为什么你改变他想要的那些私有变量的可访问性? – 2014-09-26 08:02:54

+0

我有。谢谢。 – 2014-09-26 08:04:48

+0

它不工作'='''以及为什么要包括? – Shaggy 2014-09-26 08:05:51

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)); 
+0

所以选择的答案也是不正确的 – 2014-09-26 08:17:41

+0

但是这也会失去私人的可接受性。 – 2014-09-26 08:19:01

+0

在JavaScript中没有隐私可访问性。在python中,所有东西都是暴露的,通常只需将变量重命名为_variable就可以声明一个变量为private。 有一些hackish的方式来实现一些隐私权的访问,但它从来没有真正的私人,甚至不是一个很好的做法imo。你的第一个类的声明是正确的,但如果你想序列化它不适合。 – Cyrbil 2014-09-26 08:22:49

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 = ""; 

这些意味着是私人的。保持他们那样。

+0

这是错误的,你最终在对象中有'category_id'和'this.category_id'。没有这样做的意义。 – Cyrbil 2014-09-26 08:03:30

+0

@cyrbil但它的作品! – Shaggy 2014-09-26 08:04:55

+0

@cyrbil no。看看控制台。你没有看到 - 并应该看到那些私人的 – 2014-09-26 08:05:25

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');