ocalStorage和localStorage之间有什么区别吗

ocalStorage和localStorage之间有什么区别吗

本文介绍了window.localStorage和localStorage之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做以下事情:

var store = window.localStorage;
store.setItem()

但是现在我看到了执行此操作的代码:

but now I see code doing this:

localStorage.setItem()

两者都做同一件事吗?

推荐答案

除非您在自定义定义的范围内声明了名为localStorage的变量,否则它们是相同的. localStoragewindow.localStorage.实际上,全局命名空间中的每个变量都可以作为window.<variableName>

Unless you've declared a variable named localStorage in a custom defined scope, they're the same. localStorage refers to window.localStorage. In fact every variable in global namespace can be accessed as window.<variableName>

例如:

<script>
function foo() {
    // here window.localStorage == localStorage
}
function foo2 {
    var localStorage = 10;
    // here window.localStorage != localStorage
    // since you have a local variable named localStorage
}
</script>

这篇关于window.localStorage和localStorage之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 05:36