简体   繁体   English

使用打字稿从本地存储中检索后,如何还原对象的原型?

[英]How do I restore an object's prototype after retrieving from local storage using typescript?

I have a typescript class with a getTotal() method on the prototype. 我在原型上有一个带有getTotal()方法的打字稿类。

class Score {
    roundOne: any;
    roundTwo: any;
    roundThree: any;
    roundFour: any;
    roundFive: any;
    roundSix: any;
    roundSeven: any;
    getTotal() {
      let total = 0;
      if(!isNaN(parseInt(this.roundOne))) total+=parseInt(this.roundOne);
      if(!isNaN(parseInt(this.roundTwo))) total+=parseInt(this.roundTwo);
      if(!isNaN(parseInt(this.roundThree))) total+=parseInt(this.roundThree);
      if(!isNaN(parseInt(this.roundFour))) total+=parseInt(this.roundFour);
      if(!isNaN(parseInt(this.roundFive))) total+=parseInt(this.roundFive);
      if(!isNaN(parseInt(this.roundSix))) total+=parseInt(this.roundSix);
      if(!isNaN(parseInt(this.roundSeven))) total+=parseInt(this.roundSeven);

      return total;
    }
}

The method works for my needs until I save an instance of 'Score' to localStorage and try to retrieve it. 该方法可以满足我的需求,直到将“ Score”的实例保存到localStorage并尝试检索它为止。 The prototype is stripped from the object so I no longer have access to the getTotal() method. 原型已从对象中剥离,因此我不再有权使用getTotal()方法。 Aside from restructuring my code, is there any way to reattach or point the objects to their prototype when I retrieve them from localStorage? 除了重组代码之外,当我从localStorage检索对象时,是否还有任何方法可以将对象重新附加或指向它们的原型? Something along the lines of: 类似于以下内容:

let scores = JSON.parse(localStorage.getItem('scores'));

scores.forEach(function(score){
  // reattach or point to prototype here
})

Parsing a json doesn't result in instance of classes but with simple js objects which contain the same properties. 解析json不会导致类的实例,但是会导致包含相同属性的简单js对象。

You have (at least) two ways to solve this: 您有(至少)两种方法可以解决此问题:
(1) Add a constructor which can handle this state object: (1)添加一个可以处理此状态对象的构造函数:

class Score {
    roundOne: any;
    roundTwo: any;
    ...

    constructor(state: Score) {
        this.roundOne = state.roundOne;
        this.roundTwo = state.roundTwo;
        ...
    }

    getTotal() {
        ...
    }
}

let scores = (JSON.parse(localStorage.getItem('scores')) as Score[])
    .map(score => new Score(score));

Notice that even though I use the type Score for the state objects it's not really the case, they just share the same structure (minus the method). 请注意,即使我对状态对象使用了Score类型,但实际情况并非如此,它们只是共享相同的结构 (减去方法)。 You can make an interface for that as well: 您也可以为此创建一个接口:

interface State {
    roundOne: any;
    roundTwo: any;
    ...
}

(2) Use Object.assign : (2)使用Object.assign

let scores = (JSON.parse(localStorage.getItem('scores')) as Score[])
    .map(score => Object.assign(new Score(), score));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本地存储中保存和恢复 File 对象 - How do I save and restore a File object in local storage 如何在反应 typescript 中呈现本地存储中的数据? - How do i render data from local storage in react typescript? 我可以将对象原型保存在本地存储中吗? - Can I save object prototype in local storage? 使用JavaScript保存后从浏览器本地存储中检索数据 - Retrieving data from browser local storage after saving it using javascript 检索本地存储时如何获取键值 - How do I get the key value when retrieving local storage 如何从本地存储对象访问特定属性? (角度2 /角度2-本地存储) - How do I access a specific property from a local storage object? (Angular 2/angular-2-local-storage) 从本地存储中检索数组中对象的值 - Retrieving values in an object in an array from local storage 在敲除js中使用'with'进行数据绑定后,如何访问另一个原型对象 - How do I access another prototype object after data binding using 'with' in knockout js 如何在JavaScript中迭代Object原型的属性? - How do I iterate over the properties of an Object's prototype in JavaScript? 如何从构造函数内部的原型对象中检索属性 - How do I retrieve a property from a prototype's object that is inside a constructor function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM