我正在尝试更新对象的现有实例中的元素。当我这样做时,我认为继承的一些特性会在Chrome的Dev Tools中显示出来。
//this is a Chrome Dev Tools view of the instantiated object I attempted to update/modify;
//this is rejected by a later function I'm using in the xAPI tincan-min.js library
statement.target.definition.choices: Array(5)
0:
description: {en-US: "Apple"}
id: "kcContent.questions[3].answerChoices[0]"
__proto__:
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
//this is a Dev Tools view of a "placeholder" instantiated objects
1: TinCan.InteractionComponent // how do I preserve the 'TinCan.InteractionComponent' characteristic when I update this instanced object?
description: {en-US: "pending"}
id: "pending"
__proto__:
LOG_SRC: "InteractionComponent"
asVersion: ƒ (a)
getLangDictionaryValue: ƒ (a,b)
init: ƒ (a)
log: ƒ (a,b)
__proto__: Object
//these are additional "placeholder" objects--all of which have the "TinCan.InteractionComponent" prefix, which is what I'm losing when I try to update elements using the function below
2: TinCan.InteractionComponent {id: "pending", description: {…}}
3: TinCan.InteractionComponent {id: "pending", description: {…}}
4: TinCan.InteractionComponent {id: "pending", description: {…}}
length: 5
__proto__: Array(0)
//This is the function I'm using to update the instantiated object:
function populateStatementChoices(callback){
console.log("Populating statement choices.");
statement.target.definition.choices.forEach(function(item, index) {
function setStatementChoiceID(num){
//this updates the key/values but removes 'TinCan.InteractionComponent' from the object
statement.target.definition.choices[num] = {
id:"kcContent.questions[" + choiceQuestionIndex + "].answerChoices[" + num + "]",
description:{"en-US":kcContent.questions[choiceQuestionIndex].answerChoices[num].choiceText},
}
setStatementChoiceID(index);
});
if (typeof callback === 'function'){
callback();
}
};
保留“TinCan.InteractionComponent”特性是我无法弄清楚的。希望有人以前见过这个,并指出我正确的方向。感谢您的时间和关注;我比你知道的更感谢!
您正在丢失“TinCan.Interaction Component”,因为您将阵列位置设置为新对象。
statement.target.definition.choices = [ ..., ..., ..., ...]
//your code says, access the array position at number and assign (=) whatever is below to it.
statement.target.definition.choices[num] = {
id:"kcContent.questions[" + choiceQuestionIndex + "].answerChoices[" + num + "]",
description:{"en-US":kcContent.questions[choiceQuestionIndex].answerChoices[num].choiceText},
}
首先,你应该重写你的功能。您没有正确使用forEach。我建议使用map,因为它不会改变数组,但为了简单起见,您的代码可以简化为:
function populateStatementChoices(callback){
console.log("Populating statement choices.");
statement.target.definition.choices.forEach(function(item, index) {
item.id = "kcContent.questions[" + choiceQuestionIndex + "].answerChoices[" + index+ "]";
item.description = { "en-US": kcContent.questions[choiceQuestionIndex].answerChoices[index].choiceText};
}
if (typeof callback === 'function'){
callback();
}
};
有了这个,你应该很好地使用更新而不会丢失其他属性。