Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用TypeScript创建实体对象的最佳方法?_Javascript_Typescript_Design Patterns - Fatal编程技术网

Javascript 使用TypeScript创建实体对象的最佳方法?

Javascript 使用TypeScript创建实体对象的最佳方法?,javascript,typescript,design-patterns,Javascript,Typescript,Design Patterns,我刚刚开始使用TypeScript,想知道这是否是使用来自API的数据初始化实体对象的最佳方法 我以前如何使用JavaScript: class Entity { constructor(apiResponseData = {}) { this.id = apiResponseData.id this.set = apiResponseData.set ... } toObject() { return { id: this.id, ... }

我刚刚开始使用TypeScript,想知道这是否是使用来自API的数据初始化实体对象的最佳方法

我以前如何使用
JavaScript

class Entity {
  constructor(apiResponseData = {}) {
    this.id = apiResponseData.id
    this.set = apiResponseData.set
    ...
  }

  toObject() {
    return { id: this.id, ... }
  }
}
interface EntityData {
  id: string
  set: string,
  ...
}

class Entity {
  id: string
  set: string
  
  constructor(apiResponseData: EntityData) {
    this.id = apiResponseData.id
    this.set = apiResponseData.set
    ...
  }

  toObject() {
    return { id: this.id, ... }
  }
}
对于我使用
TypeScript
的方法:

class Entity {
  constructor(apiResponseData = {}) {
    this.id = apiResponseData.id
    this.set = apiResponseData.set
    ...
  }

  toObject() {
    return { id: this.id, ... }
  }
}
interface EntityData {
  id: string
  set: string,
  ...
}

class Entity {
  id: string
  set: string
  
  constructor(apiResponseData: EntityData) {
    this.id = apiResponseData.id
    this.set = apiResponseData.set
    ...
  }

  toObject() {
    return { id: this.id, ... }
  }
}
TypeScript
中是否有更好的方法来执行此操作?由于多次声明类型,它复制了大量代码,因此需要定义的属性越多,它就越大

我想到的另一种方法可能是:

interface EntityData {
  id: string
  set: string,
  ...
}

interface Entity extends EntityData {
  toObject: () => EntityData
}

const buildEntity = (apiResponseData: EntityData) => {
  return {
    id: apiResponseData.id,
    set: apiResponseData.set,
    ...
    toObject: () => ({ id: apiResponseData.id, ... }) // imagine if method does some other operations to the data
  }
}
对于这种方法,我唯一关心的是,如果成员数据发生了更改(例如,在构建实体之后,
id=1
=>
id=2
),则
toObject()
将不正确


有什么想法或其他方法吗?

最好的方法是使用TypeScript,但有些事情可以做得更简单一些

interface EntityData {
    id: string;
    set: string,
}

// don't declare two properties,
// just create an interface
interface Entity extends EntityData {}

class Entity {
    // destruct data
    constructor({id, set}: EntityData) {
        this.id = id;
        this.set = set;
    }

    toObject(): EntityData {
        // destruct all own properties
        // (not methods in prototype)
        return {...this};
    }
}

我使用了破坏,这是ECMAScript 6标准。如果您不知道,请参阅以获取更多信息。

“我唯一关心的是,如果成员数据发生更改,则toObject()将不正确。”-只需编写
return{…,toObject(){return{id:this.id,…};}
那么您是否打算
类实体实现EntityData
?@Bergi,啊,您是对的,我使用了箭头函数,这使得
无法工作。对于实体,我将实现
EntityData
。我采用了Tomas的方法。哦,太酷了,我没有意识到一个接口和一个类有相同的名称会合并成一个,谢谢!是的,打字脚本很棒。现在,请单击此块中的复选标记以接受答案。非常感谢。