javascript/jquery-向实例化对象添加属性
我正在实例化一个构建关联数组的对象。在实例化之后,我想将属性添加到对象的成员中,但是当我尝试这样做时,我得到了一个错误 这就是这个对象,它解析一堆xml并构建场景和车辆数组:javascript/jquery-向实例化对象添加属性,javascript,jquery,arrays,object,associative-array,Javascript,Jquery,Arrays,Object,Associative Array,我正在实例化一个构建关联数组的对象。在实例化之后,我想将属性添加到对象的成员中,但是当我尝试这样做时,我得到了一个错误 这就是这个对象,它解析一堆xml并构建场景和车辆数组: var supertree = { scenes: {}, vehicles: {}, init: function() { this.parseScenes(); this.parseVehicles(); }, ... 当我实例化对象时: supertr
var supertree = {
scenes: {},
vehicles: {},
init: function() {
this.parseScenes();
this.parseVehicles();
},
...
当我实例化对象时:
supertree.init();
supertree.ready = function() {
assignSpritePos();
}
我不能给它添加属性。这就是我想做的:
function assignSpritePos(){
var sceneCount = 0;
var sceneLength = Object.keys(supertree.scenes).length;
for (i=0; i< sceneLength; i++){
//console.log(i);
supertree.scenes[i].index = i;
sceneCount++;
}
}
将属性指定给对象不是递归的,也就是说,它不会自动为您创建对象,因此您必须单独指定每个属性 试试这个
supertree.scenes[i] = { 'index': i };
将属性指定给对象不是递归的,也就是说,它不会自动为您创建对象,因此您必须单独指定每个属性 试试这个
supertree.scenes[i] = { 'index': i };
无法将属性指定给尚不存在的对象。使用以下命令:
supertree.scenes[i] = {};
supertree.scenes[i].index = i;
无法将属性指定给尚不存在的对象。使用以下命令:
supertree.scenes[i] = {};
supertree.scenes[i].index = i;
PS-与jQuery或关联数组无关。PS-与jQuery或关联数组无关。