我需要在setItem之前从localStorage中删除项目吗?

4
我有这段代码:
开发人员在setItem之前从本地存储中删除了removeItem。我觉得这样做没有必要,但我不能百分之百确定。
putCacheData = (): void => {
    this.data = {
        authenticated: this.authenticated,
        bearerToken: this.bearerToken,
        expirationDate: this.expirationDate,
        firstName: this.firstName,
        fullName: this.fullName,
        lastName: this.lastName,
        roleId: this.roleId,
        subjectId: this.subjectId,
        userName: this.userName
    };
    localStorage.removeItem('userData');
    localStorage.setItem('userData', JSON.stringify(this.data));
}

是否有可能需要使用removeItem函数?

1个回答

6

没有这样做的绝对必要。如果变量已经存在,则将被覆盖,因此在设置之前不需要删除。

唯一可能有影响的情况是在字符串化this.data时出现异常,但除非数据中有奇怪的东西(例如循环结构),否则不会发生。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接