<bdo id='j1eJI'></bdo><ul id='j1eJI'></ul>
<tfoot id='j1eJI'></tfoot>

  1. <legend id='j1eJI'><style id='j1eJI'><dir id='j1eJI'><q id='j1eJI'></q></dir></style></legend>
  2. <i id='j1eJI'><tr id='j1eJI'><dt id='j1eJI'><q id='j1eJI'><span id='j1eJI'><b id='j1eJI'><form id='j1eJI'><ins id='j1eJI'></ins><ul id='j1eJI'></ul><sub id='j1eJI'></sub></form><legend id='j1eJI'></legend><bdo id='j1eJI'><pre id='j1eJI'><center id='j1eJI'></center></pre></bdo></b><th id='j1eJI'></th></span></q></dt></tr></i><div id='j1eJI'><tfoot id='j1eJI'></tfoot><dl id='j1eJI'><fieldset id='j1eJI'></fieldset></dl></div>

  3. <small id='j1eJI'></small><noframes id='j1eJI'>

    1. localStorage 和布尔“字符串"

      时间:2023-09-04
    2. <tfoot id='Vc0of'></tfoot>
        <tbody id='Vc0of'></tbody>

      1. <small id='Vc0of'></small><noframes id='Vc0of'>

        • <bdo id='Vc0of'></bdo><ul id='Vc0of'></ul>
            1. <legend id='Vc0of'><style id='Vc0of'><dir id='Vc0of'><q id='Vc0of'></q></dir></style></legend>
                <i id='Vc0of'><tr id='Vc0of'><dt id='Vc0of'><q id='Vc0of'><span id='Vc0of'><b id='Vc0of'><form id='Vc0of'><ins id='Vc0of'></ins><ul id='Vc0of'></ul><sub id='Vc0of'></sub></form><legend id='Vc0of'></legend><bdo id='Vc0of'><pre id='Vc0of'><center id='Vc0of'></center></pre></bdo></b><th id='Vc0of'></th></span></q></dt></tr></i><div id='Vc0of'><tfoot id='Vc0of'></tfoot><dl id='Vc0of'><fieldset id='Vc0of'></fieldset></dl></div>
                本文介绍了localStorage 和布尔“字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在localStorage中存储布尔值,这个值被转换为字符串.现在尝试将此值从 localStorage 转换回布尔值,我需要使用 JSON.parse() 方法,更方便的 !! 不起作用.

                Storing boolean value in localStorage, this value is converted to string. Now trying to converting back this value from localStorage to boolean, i need to use JSON.parse() method, the more handy !! doesn't work.

                代码示例:

                var test = false;
                localStorage['test'] = test;
                console.log("JSON.parse returns: ", JSON.parse(localStorage['test']), "expected: ", test);
                console.log("'!!' returns: ", !! localStorage['test'], "expected: ", test);
                

                -jsFiddle-

                我很困惑为什么会出现这种行为.有什么解释吗?

                I'm quite confused why this behaviour. Any explaination?

                PS:在这里使用 getter/setter localStorage 方法无关紧要,结果相同.

                PS: using getter/setter localStorage methods doesn't matter here, same result.

                推荐答案

                本地存储存储字符串,恐怕不管输入是什么(如果你用一个对象喂它,它会自动转换成它的标准toString() 方法)...所以你在做 !!在字符串上测试,它总是true.

                Local storage stores strings , I'm afraid, whatever the input (if you feed it with an object, it will be converted automatically with its standard toString() method)... So you're doing !! test on a string, which is always true.

                在处理 DOM 存储

                这篇关于localStorage 和布尔“字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:JS 生成随机布尔值 下一篇:如何使 localStorage 值为 true?

                相关文章

              • <small id='pmJpI'></small><noframes id='pmJpI'>

                  • <bdo id='pmJpI'></bdo><ul id='pmJpI'></ul>
                  <i id='pmJpI'><tr id='pmJpI'><dt id='pmJpI'><q id='pmJpI'><span id='pmJpI'><b id='pmJpI'><form id='pmJpI'><ins id='pmJpI'></ins><ul id='pmJpI'></ul><sub id='pmJpI'></sub></form><legend id='pmJpI'></legend><bdo id='pmJpI'><pre id='pmJpI'><center id='pmJpI'></center></pre></bdo></b><th id='pmJpI'></th></span></q></dt></tr></i><div id='pmJpI'><tfoot id='pmJpI'></tfoot><dl id='pmJpI'><fieldset id='pmJpI'></fieldset></dl></div>
                  <tfoot id='pmJpI'></tfoot>

                    <legend id='pmJpI'><style id='pmJpI'><dir id='pmJpI'><q id='pmJpI'></q></dir></style></legend>