我有一个父元素,其中包含n个子元素。我创建了一个while loop
来检查parent.children.length !== x
。然后我会添加一个孩子,直到它相等。
哪个更有效/更快:
将parent.children.length
添加到var,然后在while循环中将其递增1.像这样:
var childrenLength = parent.children.length;
while (childrenLength !== someNumber) {
parent.appendChild(someArray[childrenLength]);
childrenLength++;
}
// Or can increment childrenLength when accessing it
while (childrenLength !== someNumber) {
parent.appendChild(someArray[childrenLength++]);
}
使用parent.children.length
。像这样:
while (parent.children.length !== someNumber) {
parent.appendChild(someArray[parent.children.length]);
}