有人可以向我解释javascript getCookie()中while循环的功能吗?

Could someone explain the function of while loop in javascript getCookie() to me?

本文关键字:循环 while 功能 可以向 解释 getCookie javascript      更新时间:2023-09-26

很抱歉有问题要问...以下是W3C学校的getCookie代码教程

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

谢谢。。。

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) 的函数是什么;

它删除了c前面的空格。

既然是循环,为什么不继续重复并卡在那里?

while循环仅在值为 true 重复。你不能有一个由无限空间组成的字符串。