Javascript 在设置项之前,我是否需要从本地存储中删除项?
我有以下代码: 开发人员在setItem之前从本地存储中执行removeItem。在我看来,没有必要这样做,但我不是100%确定:Javascript 在设置项之前,我是否需要从本地存储中删除项?,javascript,local-storage,Javascript,Local Storage,我有以下代码: 开发人员在setItem之前从本地存储中执行removeItem。在我看来,没有必要这样做,但我不是100%确定: putCacheData = (): void => { this.data = { authenticated: this.authenticated, bearerToken: this.bearerToken, expirationDate: this.expirationDate,
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));
}
是否有可能需要删除项目?不,他们绝对没有理由这样做。如果变量已经存在,那么它将被覆盖,因此无需在设置之前删除 唯一不同的方法是在对
this.data
进行字符串化的过程中出现异常,但除非这些数据中有奇怪的东西(如循环结构),否则就不会发生这种情况