2014-09-26 87 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');