ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] > [参考](https://wangdoc.com/javascript/bom/storage.html#storagegetitem) ## 概述 localStorage生命周期是永久的,除非被清除,否则永久保存 而sessionStorage仅在当前会话下有效,关闭页面或浏览器后被清除 ### 属性与方法 ``` interface Storage { readonly length: number; clear(): void; getItem(key: string): string | null; key(index: number): string | null; removeItem(key: string): void; setItem(key: string, value: string): void; [name: string]: any; } ``` ## 实例 ### 三种写法 ``` // 下面三种写法等价 window.localStorage.foo = '123'; window.localStorage['foo'] = '123'; window.localStorage.setItem('foo', '123'); ···