javascript:如何通过“属性引用”工作

发布于 2024-10-13 08:16:39 字数 526 浏览 8 评论 0原文

我有这个问题: 我用 json 模型创建了一个“通用”对象来威胁。 我需要通过他的“字符串名称”引用该模型的属性。 问题是该属性是值类型而不是对象类型,因此我丢失了引用并且更改将不会传播。

示例:

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

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

好的,效果很好,但是这种情况怎么样:?

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

我怎样才能以更好的方式做到这一点?

对大家有很多帮助。

i have this question:
I have made a "generic" object to threat with json models.
I need to pass by reference a property of this model by his 'string name'.
The problem is that the property is a value type not an object type so i lose the reference and the changes will not be propagated.

Example:

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 it work well but what about this situation:?

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

how can i do that in better way?

Tnx a lot to all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩套路吗 2024-10-20 08:16:39

这是“邪恶”的解决方案,但它有效:)

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

This is the "Evil" solution, but it works :)

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);
画骨成沙 2024-10-20 08:16:39

好吧,这是一个不太邪恶的解决方案,它也有效,至少对于您这里的场景来说......

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

OK, this is the not so evil solution and it works too, at least for the scenario that you have here...

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);
晚风撩人 2024-10-20 08:16:39

这是我的解决方案:

用法:

alert(CommonUt.GetValueProperty({"Mammal" :{"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;
}

};

This is my solution:

Usages:

alert(CommonUt.GetValueProperty({"Mammal" :{"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;
}

};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文