方括号语法与localStorage的函数 [英] Square bracket syntax vs functions for localStorage

查看:256
本文介绍了方括号语法与localStorage的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两段代码有什么区别?方括号语法是旧的,不推荐使用的语法吗?当我第一次使用localStorage时,我发现的所有文档肯定都说使用方括号语法,但现在我根本找不到任何文档。

What is the difference between the following two snippets of code? Is the square bracket syntax an old, deprecated syntax? When I first used localStorage, all the documentation I found definitely said to use the square bracket syntax, but now I can't find any documentation on it at all.

记录语法:

localStorage.setItem('hello', 'world');
localStorage.getItem('hello'); // world

方括号语法:

localStorage.hello = 'world';
localStorage.hello; // world


推荐答案

这是文档说明了这一点:


虽然可以使用标准JavaScript属性访问方法设置和读取值,但建议使用getItem和setItem方法。

Although the values can be set and read using the standard JavaScript property access method, using the getItem and setItem methods is recommended.

这篇关于方括号语法与localStorage的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆