我正在开发一个开源项目,我不想更改线路...
this.model[this.config.childrenPropertyName].push(child.model);
至
this.model[this.config.childrenPropertyName].splice(this.model.length, 0, child.model);
但是它破坏了对象模型上的顺序(这是一个树实现)。但是当我在测试工具中测试这两种方法时,它们似乎保留了顺序。我认为按照上面的方式实现,他们会做完全相同的事情......
注意:这只是我的一个愚蠢的编码错误,下面正确回答的两者之间没有区别
最佳答案
第一行插入 child.model
作为 this.model[this.config.childrenPropertyName]
数组的最后一个元素。
第二行将 child.model
插入到 this.model[this.config.childrenPropertyName]
数组内的某个位置。
仅当 this.model[this.config.childrenPropertyName].length
等于 this.model.length
时,这些行才相同,但第一行要简洁得多。
关于javascript - 此场景中 array.push 和 array.splice 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834632/