当我保存null/undefined
时值进入localStorage
。我使用 localStorage.getItem
时收到的结果将是一个字符串 ( "null" or "undefined"
)
localStorage.setItem("item1", null)
localStorage.setItem("item2", undefined)
const item1 = localStorage.getItem("item1")
const item2 = localStorage.getItem("item2")
console.log(item1) // The result will be "null" not null
console.log(item2) // The result will be "undefined" not undefined
谁能帮我解释一下这个案例!
最佳答案
Js 将其字符串化到本地存储
storage.setItem(keyName, keyValue)
keyName and KeyValue both are of type DOMString
DOMString is a UTF-16 string. As JavaScript already uses such strings, DOMString is mapped directly to a String.
Passing null to a method or parameter accepting a DOMString typically stringifies to "null"
关于javascript - 为什么 localStorage 返回大小写为 null 且未定义的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690917/