简体   繁体   English

重构javascript for循环

[英]Refactoring javascript 'for' loop

I am practicing my javascript. 我正在练习我的JavaScript。 I have created a link to show hide paragraphs. 我创建了一个链接来显示隐藏段落。 The code currently uses 2 'for' loops. 该代码当前使用2个“ for”循环。 Should I somehow be creating a function for the 'for' loop and then re-use the function? 我是否应该以某种方式为“ for”循环创建一个函数,然后重新使用该函数?

var paragraphs = document.getElementsByTagName('p'),
    firstParagraph = paragraphs[0],
    link = document.createElement('a');
link.innerHTML = 'Show more';
link.setAttribute('class', 'link');
link.setAttribute('href', '#');
firstParagraph.appendChild(link);

for (var i = 1; i <= paragraphs.length - 1; i++) {
    paragraphs[i].classList.add('hide')
}

function toggleHide(e) {
    e.preventDefault;
    var paragraphs = document.getElementsByTagName('p');
    for (i = 1; i <= paragraphs.length - 1; i++) {
        paragraphs[i].classList.toggle('hide');
    }
}

link.addEventListener('click', toggleHide)

Since toggle('hide') will also do the same thing of add('hide') when initializing the paragraph list, it is good to pull up the duplicate code to a single function. 由于在初始化段落列表时,toggle('hide')也会执行add('hide')的相同操作,因此最好将重复的代码拉到单个函数中。

For example: 例如:

var paragraphs = document.getElementsByTagName('p'),
firstParagraph = paragraphs[0],
link = document.createElement('a');
link.innerHTML = 'Show more';
link.setAttribute('class' , 'link');
link.setAttribute('href' , '#');
firstParagraph.appendChild(link);
toggleHideAll();

function toggleHide( e ){
    e.preventDefault;
    var paragraphs = document.getElementsByTagName('p');
    toggleHideAll();
}

function toggleHideAll(){
    for( i = 1 ; i <= paragraphs.length-1 ; i++){
        paragraphs[i].classList.toggle('hide');
    }  
}

link.addEventListener( 'click' , toggleHide) 

Yes, a single loop to achieve both ends would be good, as @Solmon says: 是的,一个实现两端的循环会很好,如@Solmon所说:

function toggleHideAll(){
    for (var i = 1; i <= paragraphs.length-1; i++) {
        paragraphs[i].classList.toggle('hide');
    }  
}

There is a more idiomatic way to express this loop, however, and I would advise you to use it, because the original form is confusing to developers who are accustomed to the standard form: 但是,还有一种更惯用的方式来表达此循环,我建议您使用它,因为原始格式会使习惯于标准格式的开发人员感到困惑:

function toggleHideAll() {
    for (var i = 0; i < paragraphs.length; i++) {
        paragraphs[i].classList.toggle('hide');
    }  
}

That is, loop starting at zero, while the loop variable is less than length (not less than or equal to length minus one. And in this case, the loop does not do exactly the same as your original, because the original actually skips your first paragraph. If that's intentional, rather than tweaking the loop parameters, I would recommend toggling all of the paragraphs and then handling the special case with a line of code outside the loop: 也就是说,循环从零开始,而循环变量小于 length(不小于或等于 length减一)。在这种情况下,循环的作用与您的原始循环并不完全相同,因为原始循环实际上跳过了您的循环第一段,如果不是故意的,而不是调整循环参数,我建议切换所有段落,然后在循环外使用一行代码来处理特殊情况:

function toggleHideAll() {
    for (var i = 0; i < paragraphs.length; i++) {
        paragraphs[i].classList.toggle('hide');
    }  
    paragraphs[0].classList.remove('hide');
}

Also, it's really nice when you can avoid explicit loops in your code altogether: 此外,当您可以完全避免代码中的显式循环时,这真的很不错:

function toggleHideAll() {
    paragraphs.forEach(p => p.classList.toggle('hide'));
    paragraphs[0].classList.remove('hide');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM