简体   繁体   English

JS:“false”可以强制类型为 false 吗?

[英]JS: Can “false” be type-coerced to false?

When you use Html5 localStorage values are stored as strings.当您使用 Html5 localStorage 值存储为字符串。 This is something you need to deal with if you want to store the state of a checkbox and then restore it at a later date.如果您想存储复选框的 state 然后在以后恢复它,这是您需要处理的事情。 I was hoping that something like this would work:我希望这样的事情会起作用:

(function() {
  function e(id) { return document.getElementById(id); }

  e('save').addEventListener('click', function() {
   localStorage['check-value'] = e('checkbox').checked
  })
  e('checkbox').checked = localStorage['check-value'];
})();

But it seems that 'check-value' will store something along the lines of "false" if the box is not checked and the string "false" gets type-coerced to true on that second-to-last line.但是,如果未选中该框并且字符串"false"在倒数第二行上被强制类型强制为true ,则“check-value”似乎将存储"false"行中的某些内容。 I know that I could make a little if-test but this is for a hobby project and I want to figure it out if possible.我知道我可以做一些 if-test,但这是一个爱好项目,如果可能的话我想弄清楚。 Can "false" be coerced to false ?可以将"false"强制为false吗?

I'm not using libraries for this btw because its a chrome extension and I want to keep it lightweight.我没有为这个 btw 使用库,因为它是一个 chrome 扩展,我想让它保持轻量级。

"false" is just a string like any other, you would need to do your own check here. "false" 只是一个字符串,就像其他字符串一样,您需要在此处进行自己的检查。

You can test whether it's equal to the string "false":您可以测试它是否等于字符串“false”:

e('checkbox').checked = localStorage['check-value'] != 'false';

Alternatively, you can coerce it to a number before you store it and after you retrieve it:或者,您可以在存储它之前和检索它之后将其强制转换为一个数字:

localStorage['check-value'] = +e('checkbox').checked
// ...
e('checkbox').checked = +localStorage['check-value']

You can try using JSON.parse to pull out the value.您可以尝试使用JSON.parse提取值。 Ideally you would also use JSON.stringify to serialize objects etc. You can make this more manageable by abstracting it away behind some other interface.理想情况下,您还可以使用JSON.stringify来序列化对象等。您可以通过将其抽象到其他接口后面来使其更易于管理。

JSON.parse("false") == false

Example for a wrapper:包装器示例:

var LocalStorageManager = new (function () {
   this.set = function (key, object) {
      localStorage[key] = JSON.stringify(object);
   };

   this.get = function (key) {
      return JSON.parse(localStorage[key]);
   };
});

You can't really do that on JS.你不能在 JS 上真正做到这一点。 You should use var boolValue = (stringValue !== 'false') or whatever floats your boat.您应该使用var boolValue = (stringValue !== 'false')或任何让您的船漂浮的东西。

Change this line:更改此行:

e('checkbox').checked = localStorage['check-value'];

to this:对此:

e('checkbox').checked = localStorage['check-value'] == "true";

or depending upon what you want:或取决于你想要什么:

e('checkbox').checked = localStorage['check-value'] != "false";

If you do this more than a couple times, wrap the boolean local storage access in a tiny little function that returns only boolean true or false.如果您这样做超过几次,则将 boolean 本地存储访问包装在一个很小的 function 中,它只返回 boolean 真或假。

function tfStr(val) {
    return(val === "true");
}

Just riffing here but you could do something really stupid like只是在这里重复,但你可以做一些非常愚蠢的事情,比如

eval(localStorage['check-value'])

If the string is false it will evaluate to the boolean false.如果字符串为假,它将评估为 boolean 假。 Obviously you don't want to do that since it's a major security hole.显然你不想这样做,因为这是一个主要的安全漏洞。

Alternatively you could use a ternary operator:或者,您可以使用三元运算符:

(localStorage['check-value']==="false")?false:localStorage['check-value'];

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

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