Javascript 构造函数中的复合属性
我想构建一个如下所示的对象数组:Javascript 构造函数中的复合属性,javascript,Javascript,我想构建一个如下所示的对象数组: var someObject = { id, groupA { propertyA: 0, propertyB: 0, }, groupB { propertyA: 0, propertyB: 0 totals {} } var SomeType = function(){ this.id = 0; this.groupA = { propertyA: 0, property
var someObject = {
id,
groupA {
propertyA: 0,
propertyB: 0,
},
groupB {
propertyA: 0,
propertyB: 0
totals {}
}
var SomeType = function(){
this.id = 0;
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.total = {};
}
SomeType.prototype = {
constructor: SomeType
}
Object.defineProperty(SomeType.prototype, 'propertyC', {
get: function(){ return this.groupA.propertyA + this.groupA.propertyB }
});
并添加以下复合属性:
Object.defineProperty(someObject.groupA, "propertyC",
{
get: function() {
return someObject.groupA.propertyA + someObject.groupA.propertyB;
}
});
并使用相同的方法添加属性:
->groupB.propertyC
groupB.propertyA+groupB.propertyB
->totals.propertyA
groupA.propertyA+groupB.propertyA
->totals.propertyB
groupA.propertyB+groupB.propertyB
->totals.propertyC
groupA.propertyC+groupB.propertyC
这有意义吗?有可能吗?如果有,怎么可能?我不知道是否理解你的问题;但一般来说,当您有想要在特定类型的所有实例中共享的成员时,您应该将它们放入构造函数的原型中 在您的示例中,您使用的是object literal,这并不容易做到,除非您扩展了对象构造函数的原型,我不推荐这样做 这样做怎么样:
var someObject = {
id,
groupA {
propertyA: 0,
propertyB: 0,
},
groupB {
propertyA: 0,
propertyB: 0
totals {}
}
var SomeType = function(){
this.id = 0;
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.total = {};
}
SomeType.prototype = {
constructor: SomeType
}
Object.defineProperty(SomeType.prototype, 'propertyC', {
get: function(){ return this.groupA.propertyA + this.groupA.propertyB }
});
我不知道是否理解你的问题;但一般来说,当您有想要在特定类型的所有实例中共享的成员时,您应该将它们放入构造函数的原型中 在您的示例中,您使用的是object literal,这并不容易做到,除非您扩展了对象构造函数的原型,我不推荐这样做 这样做怎么样:
var someObject = {
id,
groupA {
propertyA: 0,
propertyB: 0,
},
groupB {
propertyA: 0,
propertyB: 0
totals {}
}
var SomeType = function(){
this.id = 0;
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.total = {};
}
SomeType.prototype = {
constructor: SomeType
}
Object.defineProperty(SomeType.prototype, 'propertyC', {
get: function(){ return this.groupA.propertyA + this.groupA.propertyB }
});
我不知道是否理解你的问题;但一般来说,当您有想要在特定类型的所有实例中共享的成员时,您应该将它们放入构造函数的原型中 在您的示例中,您使用的是object literal,这并不容易做到,除非您扩展了对象构造函数的原型,我不推荐这样做 这样做怎么样:
var someObject = {
id,
groupA {
propertyA: 0,
propertyB: 0,
},
groupB {
propertyA: 0,
propertyB: 0
totals {}
}
var SomeType = function(){
this.id = 0;
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.total = {};
}
SomeType.prototype = {
constructor: SomeType
}
Object.defineProperty(SomeType.prototype, 'propertyC', {
get: function(){ return this.groupA.propertyA + this.groupA.propertyB }
});
我不知道是否理解你的问题;但一般来说,当您有想要在特定类型的所有实例中共享的成员时,您应该将它们放入构造函数的原型中 在您的示例中,您使用的是object literal,这并不容易做到,除非您扩展了对象构造函数的原型,我不推荐这样做 这样做怎么样:
var someObject = {
id,
groupA {
propertyA: 0,
propertyB: 0,
},
groupB {
propertyA: 0,
propertyB: 0
totals {}
}
var SomeType = function(){
this.id = 0;
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.groupA = {
propertyA: 0,
propertyB: 0
};
this.total = {};
}
SomeType.prototype = {
constructor: SomeType
}
Object.defineProperty(SomeType.prototype, 'propertyC', {
get: function(){ return this.groupA.propertyA + this.groupA.propertyB }
});
这是可以做到的。您只需要确保groupA和groupB继承自具有composite属性的对象
var proto = {};
Object.defineProperty(proto, 'propertyC', {
get : function() { return this.propertyA + this.propertyB; }
});
var someObj = {
id : '1',
groupA : Object.create(proto, {
propertyA : { value : 1 }, propertyB : { value : 2 }
}),
groupB : Object.create(proto, {
propertyA : { value : 3 }, propertyB : { value : 4 }
}),
totals : Object.create(proto, {
propertyA : { get : function() { return someObj.groupA.propertyA + someObj.groupB.propertyA; } },
propertyB : { get : function() { return someObj.groupA.propertyB + someObj.groupB.propertyB; } }
})
}
// Usage:
console.log(someObj.groupA.propertyC); // 3
console.log(someObj.groupB.propertyC); // 7
console.log(someObj.totals.propertyC); // 10
这是可以做到的。您只需要确保groupA和groupB继承自具有composite属性的对象
var proto = {};
Object.defineProperty(proto, 'propertyC', {
get : function() { return this.propertyA + this.propertyB; }
});
var someObj = {
id : '1',
groupA : Object.create(proto, {
propertyA : { value : 1 }, propertyB : { value : 2 }
}),
groupB : Object.create(proto, {
propertyA : { value : 3 }, propertyB : { value : 4 }
}),
totals : Object.create(proto, {
propertyA : { get : function() { return someObj.groupA.propertyA + someObj.groupB.propertyA; } },
propertyB : { get : function() { return someObj.groupA.propertyB + someObj.groupB.propertyB; } }
})
}
// Usage:
console.log(someObj.groupA.propertyC); // 3
console.log(someObj.groupB.propertyC); // 7
console.log(someObj.totals.propertyC); // 10
这是可以做到的。您只需要确保groupA和groupB继承自具有composite属性的对象
var proto = {};
Object.defineProperty(proto, 'propertyC', {
get : function() { return this.propertyA + this.propertyB; }
});
var someObj = {
id : '1',
groupA : Object.create(proto, {
propertyA : { value : 1 }, propertyB : { value : 2 }
}),
groupB : Object.create(proto, {
propertyA : { value : 3 }, propertyB : { value : 4 }
}),
totals : Object.create(proto, {
propertyA : { get : function() { return someObj.groupA.propertyA + someObj.groupB.propertyA; } },
propertyB : { get : function() { return someObj.groupA.propertyB + someObj.groupB.propertyB; } }
})
}
// Usage:
console.log(someObj.groupA.propertyC); // 3
console.log(someObj.groupB.propertyC); // 7
console.log(someObj.totals.propertyC); // 10
这是可以做到的。您只需要确保groupA和groupB继承自具有composite属性的对象
var proto = {};
Object.defineProperty(proto, 'propertyC', {
get : function() { return this.propertyA + this.propertyB; }
});
var someObj = {
id : '1',
groupA : Object.create(proto, {
propertyA : { value : 1 }, propertyB : { value : 2 }
}),
groupB : Object.create(proto, {
propertyA : { value : 3 }, propertyB : { value : 4 }
}),
totals : Object.create(proto, {
propertyA : { get : function() { return someObj.groupA.propertyA + someObj.groupB.propertyA; } },
propertyB : { get : function() { return someObj.groupA.propertyB + someObj.groupB.propertyB; } }
})
}
// Usage:
console.log(someObj.groupA.propertyC); // 3
console.log(someObj.groupB.propertyC); // 7
console.log(someObj.totals.propertyC); // 10
不使用嵌套结构,不。原型只知道
总计
对象,不知道某个对象
不使用嵌套结构,不。原型只知道总计
对象,不知道某个对象
不使用嵌套结构,不。原型只知道总计
对象,而不知道某个对象
不使用嵌套结构,不。原型只知道总计
对象,不是关于someObject
那么totals
呢?@Bergi我添加了总数。我想我是从你的答案开始的,但你缺少totals.propertyC,我找不到解决方案。我开始认为我可能需要重新考虑我的数据结构。要么全部使用,要么只使用.totals属性。@carlsb3rgtotals.propertyC
可以正常工作,正如您在上一个console.log中看到的那样。它是从proto继承来的,是的。我现在明白了,但是我仍然需要为每个属性创建新的函数,除了那些来自原型的函数。我想我可以将总计作为一个函数来执行,该函数将属性的名称作为一个参数。如果没有人有更好的建议,我会接受你的答案。总数如何?@Bergi我添加了总数。我想我是从你的答案开始的,但你缺少totals.propertyC,我找不到解决办法。我开始认为我可能需要重新考虑我的数据结构。要么全部使用,要么只使用.totals属性。@carlsb3rgtotals.propertyC
可以正常工作,正如您在上一个console.log中看到的那样。它是从proto继承来的,是的。我现在明白了,但是我仍然需要为每个属性创建新的函数,除了那些来自原型的函数。我想我可以将总计作为一个函数来执行,该函数将属性的名称作为一个参数。如果没有人有更好的建议,我会接受你的答案。总数如何?@Bergi我添加了总数。我想我是从你的答案开始的,但你缺少totals.propertyC,我找不到解决办法。我开始认为我可能需要重新考虑我的数据结构。要么全部使用,要么只使用.totals属性。@carlsb3rgtotals.propertyC
可以正常工作,正如您在上一个console.log中看到的那样。它是从proto继承来的,是的。我现在明白了,但是我仍然需要为每个属性创建新的函数,除了那些来自原型的函数。我想我可以将总计作为一个函数来执行,该函数将属性的名称作为一个参数。如果没有人有更好的建议,我会接受你的答案。总数如何?@Bergi我加了总数。我想我用你的答案有所收获了