javascript:如何通过';财产参考';
我有一个问题: 我已经用json模型制作了一个“通用”对象来应对威胁。 我需要通过引用通过他的“字符串名”传递此模型的属性。 问题是属性是值类型而不是对象类型,因此我丢失了引用,并且更改将不会传播 例如:javascript:如何通过';财产参考';,javascript,generics,properties,pass-by-reference,Javascript,Generics,Properties,Pass By Reference,我有一个问题: 我已经用json模型制作了一个“通用”对象来应对威胁。 我需要通过引用通过他的“字符串名”传递此模型的属性。 问题是属性是值类型而不是对象类型,因此我丢失了引用,并且更改将不会传播 例如: function Manager(json){this.JsonModel = json;} Manager.prototype.Increment = function(propertyName){ this.JsonModel[propertyName]++; } var manag
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"));
我怎样才能做得更好
这是一个“邪恶”的解决方案,但它确实有效:)
好的,这是一个不那么邪恶的解决方案,它也很有效,至少在你这里的场景中是这样的
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);
函数A(json)
{
this.Data=json;
}
A.prototype.inc=功能(道具)
{
var d=这个数据;
var s=资产拆分(“.”);
对于(变量i=0;i/***
* 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;
}
/***
*检查obj中是否存在propertyName。
*PropertyName可以是带有“点”分隔符的字符串
*例如:ContainProperty(json,“哺乳动物.狗”);
*@param obj搜索属性的对象
*@param propertyName{string}属性的名称
*/
ContainProperty:函数(obj,propertyName){
如果(!IsNotNullObject(obj)){
返回错误
}
如果(!IsNotEmptyString(propertyName)){
抛出新错误(“我无法检查空属性名”);
}
if(propertyName.indexOf('.')=-1){
返回(obj中的propertyName);
}
var-refObj=obj;
var=true;
$.each(propertyName.split('.')、函数(i、项){
如果(!(REFBJ中的项目)){
成立=假;
返回false;
}
REFBJ=REFBJ[项目];
});
成立回归基金;
},
/***
*获取属性(或子属性)的值
*警告:如果属性的值为“值类型”,则不会传播任何更改!
*@param obj{object}
*@param propertyName{string}属性名称。对于点分隔的'deep'属性:哺乳动物.Dog
*/
GetValueProperty:函数(obj,propertyName){
如果(!CommonUt.ContainProperty(obj,propertyName)){
抛出新错误(“如果属性不存在,则无法检索属性引用!”);
}
if(propertyName.indexOf('.')=-1){
返回对象[propertyName];
}
var-refObj=obj;
$.each(propertyName.split('.')、函数(i、项){
REFBJ=REFBJ[项目];
});
返回REBOJ;
},
/***
*要使用值属性进行威胁,请使用
*@param obj
*@param propertyName
*@param值
*/
SetValueProperty:函数(对象、属性名称、值){
如果(!CommonUt.ContainProperty(obj,propertyName)){
抛出新错误(“如果属性不存在,则无法检索属性引用!”);
}
if(propertyName.indexOf('.')=-1){
obj[propertyName]=值;
返回;
}
var-refObj=obj;
var slices=propertyName.split('.');
对于(变量i=0;i<(slices.length-1);i++){
refObj=refObj[切片[i]];
}
refObj[slices[slices.length-1]=值;
}
}) ehhehe无恶tnx;)这是我试图避免的事情:为了清晰起见,P(建议使用tnx):这里没有JSON,JSON只是对象的字符串表示;这里处理的是纯JavaScript对象,用对象文字表示法编写(≠ ).Tnx Marcel Korpel:我的错误:你是对的!!我们是同步的!我刚刚完成实现一个类似的解决方案!现在我发布了它!Tnx很多。(ps:这不是魔鬼,但我也不喜欢:(
/***
* 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;
}