有人可以向我解释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.