有人可以向我解释while循环在javascript getCookie()中的功能吗? [英] Could someone explain the function of while loop in javascript getCookie() to me?

查看:49
本文介绍了有人可以向我解释while循环在javascript getCookie()中的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉提出一个问题...以下是W3C学校的getCookie代码教程

sorry for a question to ask... Below is the tutorial of getCookie code from W3C School

有人会教我 while(c.charAt(0)=='')c = c.substring(1); 的作用是什么,既然是while循环,为什么会不会一直重复并卡在那儿?

Would someone teach me what is the function of while (c.charAt(0)==' ') c = c.substring(1); , and since it is while loop, why won't it keep repeating and stuck there?

谢谢...

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
    }

   }

推荐答案

while(c.charAt(0)=='')的功能是什么?c = c.substring(1);

what is the function of while (c.charAt(0)==' ') c = c.substring(1);

它将删除 c 前面的空格.

由于它是while循环,为什么不继续重复并停留在那里?

since it is while loop, why won't it keep repeating and stuck there?

while 循环仅重复 while ,该值为true.您不能有由无限个空格组成的字符串.

while loops only repeat while the value is true. You can't have a string made up of infinite spaces.

这篇关于有人可以向我解释while循环在javascript getCookie()中的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆