2011-01-21 7 views
1

私はこの質問を受けています: jsonモデルで脅威になる「汎用」オブジェクトを作成しました。 このモデルのプロパティを 'string name'で参照渡しする必要があります。 問題は、プロパティがオブジェクト型ではない値型であるため、参照が失われ、変更が伝達されないことです。javascript: 'プロパティリファレンス'で作業する方法

例:

function Manager(json){this.JsonModel = json;} 
Manager.prototype.Increment = function(propertyName){ 
    this.JsonModel[propertyName]++; 
} 

var manager = new Manager({"a" : 5}); 
alert(manager.Increment("a")); 

OK、それはうまく動作しますが、どのようなこの状況について:?

var manager = new Manager({"a" : {"a1" : 5 }}); 
alert(manager.Increment("a.a1")); 

どのようにすればよいのですか?

Tnx

+1

わかりやすくするためにここにはJSONはありません.JSONはオブジェクトの文字列表現に過ぎません。ここでは、オブジェクトのリテラル表記法(≠[JSON](http://json.org))で書かれたプレーンJavaScriptオブジェクトを扱っています。 –

+0

Tnx Marcel Korpel:私の間違い:あなたは正しいです! – PadovaBoy

答えて

1

これは私のソリューションです:

用途:

警告(CommonUt.GetValueProperty({ "ママmal ":{" Dog ":{" Value ":5}}}、" Mammal.Dog.Value ")); CommonUt.SetValueProperty({"Mammal":{"Dog":{"Value":5}}、 "Mammal.Dog.Value"、6);

VAR CommonUt = {

/*** 
* Check if the propertyName is present in obj. 
* PropertyName can be a string with 'dot' separator for deepest property 
* Ex: ContainProperty(json, "Mammal.Dog"); 
* @param obj The object where search the property 
* @param propertyName {string} the name of the property 
*/ 
ContainProperty : function(obj, propertyName) { 
    if (!IsNotNullObject(obj)) { 
     return false 
    } 
    if (!IsNotEmptyString(propertyName)) { 
     throw new Error("I cannot check for an empty property name."); 
    } 
    if (propertyName.indexOf('.') === -1) { 
     return (propertyName in obj); 
    } 
    var refObj = obj; 
    var founded = true; 
    $.each(propertyName.split('.'), function(i, item) { 
     if (!(item in refObj)) { 
      founded = false; 
      return false; 
     } 
     refObj = refObj[item]; 
    }); 
    return founded; 
}, 

/*** 
* Get the value of a property (or sub-property) 
* WARN: if the value of the property is 'value-type' any changes will not be propagated! 
* @param obj {object} 
* @param propertyName {string} Property name. For 'deep' property split by dots: Mammal.Dog 
*/ 
GetValueProperty : function(obj, propertyName) { 
    if (!CommonUt.ContainProperty(obj, propertyName)) { 
     throw new Error("I cannot retrieve the property reference if the property doesen't exists!"); 
    } 
    if (propertyName.indexOf('.') === -1) { 
     return obj[propertyName]; 
    } 
    var refObj = obj; 
    $.each(propertyName.split('.'), function(i, item) { 
     refObj = refObj[item]; 
    }); 
    return refObj; 
}, 

/*** 
* To threat with value properties, use this 
* @param obj 
* @param propertyName 
* @param value 
*/ 
SetValueProperty : function(obj, propertyName, value) { 
    if (!CommonUt.ContainProperty(obj, propertyName)) { 
     throw new Error("I cannot retrieve the property reference if the property doesen't exists!"); 
    } 
    if (propertyName.indexOf('.') === -1) { 
     obj[propertyName] = value; 
     return; 
    } 
    var refObj = obj; 
    var slices = propertyName.split('.'); 
    for (var i = 0; i < (slices.length - 1); i++) { 
     refObj = refObj[slices[i]]; 
    } 
    refObj[slices[slices.length-1]] = value; 
} 

}。

1

これは「悪」なソリューションですが、それは動作します:)

function A(json) { 
    this.Data = json; 
} 

A.prototype.inc = function(prop) { 
    //OMG, It's eval!!! NOOO 
    eval("this.Data." + prop + "++"); 
} 

var p = new A({a : { c : 5 }, b: 2}); 

p.inc("b"); 
alert(p.Data.b); 
p.inc("a.c"); 
alert(p.Data.a.c); 
+0

ehhehe no evil tnx;)それは私が避けようとしている事です:P(提案のためのtnx) – PadovaBoy

1

OK、これはそう悪ではないソリューションであり、それは少なくとも、あなたがここに持っているシナリオでは、あまりにも動作します...

function A(json) 
{ 
    this.Data = json; 
} 

A.prototype.inc = function(prop) 
{ 
    var d = this.Data; 

    var s = prop.split("."); 

    for (var i=0; i < s.length - 1; i++) 
    { 
     d = d[s[i]]; 
    } 

    d[s[i]]++; 
} 

    var p = new A({ a : { b : { c : 5 }}}); 

p.inc("a.b.c"); 

alert(p.Data.a.b.c); 
+0

!私たちは同期しています!私はちょうど同様のソリューションを実装するのを完了!今私はそれを投稿する!たくさんのTnx。 (ps:それは悪魔ではないが、私も好きではない:( – PadovaBoy

関連する問題