在打字稿中动态创建对象
Dynamically creating objects in typescript
我正在尝试根据传递给它的参数的数量和类型创建动态数量的对象
我的大体骨架是这样的
interface KeyValue {
key: string;
value: string;
}
class CreateObjects {
//example of how im tyring to make the objects look
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
//function that creates objects like above but does so dynamically with unique and iterative property names
methodTest: ([dynamic number of arguments]){
[dynamicproperty1]: KeyValue = {key: 'Example', value: argument};
}
我不知道我这样做是不是全错了,但基本上我不知道会通过多少参数。它可以是 none、1 或 50+。该参数仅更改 'value' key/value 对,并且该对象需要作为其他函数的参数可访问。
我觉得我缺少一些关于创建对象的基本知识,这使得这项任务变得如此困难
谢谢
您需要几件作品来解决这个难题。
首先,我们需要添加允许我们在 class 上设置任何 属性 的类型。我们可以这样做:
class CreateObjects {
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
[key: string]: any;
当我们定义我们的方法时,我们希望将所有参数作为一个数组。我们可以使用 ...args
,它创建一个名为 args 的数组,每个参数都传递给该数组:
methodTest(...args: any[]) { }
我没有使用名称“arguments”,因为这是 JavaScript 中的一个 reserved keyword。它可以用来解决这个问题,但我认为使用 TypeScript 时会更复杂。
最后,我们需要遍历每个参数,并在class 上设置一个属性。我们可以这样做:
for (let i = 0; i < args.length; i = i + 1) {
this['Example' + i] = {key: 'Example', value: args[i]};
}
希望您能识别 for 循环。我们正在使用“this”访问 class 上的 属性。这种方法的一个警告是,如果该方法是从 class 外部调用的,“this”绑定将不同,并且属性可能设置在其他东西上。
最后,将所有内容放在一起,底部有一些测试代码以查看其工作情况:
interface KeyValue {
key: string;
value: string;
}
class CreateObjects {
//example of how im tyring to make the objects look
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
[dynamicProperty: string]: any;
//function that creates objects like above but does so dynamically with unique and iterative property names
methodTest(...args: any[]) {
for (let i = 0; i < args.length; i = i + 1) {
this['Example' + i] = {key: 'Example', value: args[i]};
}
}
}
const example = new CreateObjects();
example.methodTest("a", 1);
console.log(example.Example0);
console.log(example.Example1);
我不会说我特别喜欢像这样在 class 上设置属性。您可能想考虑是否可以更改设计以提高从 TypeScript 获得的安全性。例如,您是否可以使用单个 属性 来存储这些对象的数组?
我正在尝试根据传递给它的参数的数量和类型创建动态数量的对象
我的大体骨架是这样的
interface KeyValue {
key: string;
value: string;
}
class CreateObjects {
//example of how im tyring to make the objects look
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
//function that creates objects like above but does so dynamically with unique and iterative property names
methodTest: ([dynamic number of arguments]){
[dynamicproperty1]: KeyValue = {key: 'Example', value: argument};
}
我不知道我这样做是不是全错了,但基本上我不知道会通过多少参数。它可以是 none、1 或 50+。该参数仅更改 'value' key/value 对,并且该对象需要作为其他函数的参数可访问。
我觉得我缺少一些关于创建对象的基本知识,这使得这项任务变得如此困难
谢谢
您需要几件作品来解决这个难题。
首先,我们需要添加允许我们在 class 上设置任何 属性 的类型。我们可以这样做:
class CreateObjects {
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
[key: string]: any;
当我们定义我们的方法时,我们希望将所有参数作为一个数组。我们可以使用 ...args
,它创建一个名为 args 的数组,每个参数都传递给该数组:
methodTest(...args: any[]) { }
我没有使用名称“arguments”,因为这是 JavaScript 中的一个 reserved keyword。它可以用来解决这个问题,但我认为使用 TypeScript 时会更复杂。
最后,我们需要遍历每个参数,并在class 上设置一个属性。我们可以这样做:
for (let i = 0; i < args.length; i = i + 1) {
this['Example' + i] = {key: 'Example', value: args[i]};
}
希望您能识别 for 循环。我们正在使用“this”访问 class 上的 属性。这种方法的一个警告是,如果该方法是从 class 外部调用的,“this”绑定将不同,并且属性可能设置在其他东西上。
最后,将所有内容放在一起,底部有一些测试代码以查看其工作情况:
interface KeyValue {
key: string;
value: string;
}
class CreateObjects {
//example of how im tyring to make the objects look
propertyTest: KeyValue = {key: 'Example', value: 'Object'};
[dynamicProperty: string]: any;
//function that creates objects like above but does so dynamically with unique and iterative property names
methodTest(...args: any[]) {
for (let i = 0; i < args.length; i = i + 1) {
this['Example' + i] = {key: 'Example', value: args[i]};
}
}
}
const example = new CreateObjects();
example.methodTest("a", 1);
console.log(example.Example0);
console.log(example.Example1);
我不会说我特别喜欢像这样在 class 上设置属性。您可能想考虑是否可以更改设计以提高从 TypeScript 获得的安全性。例如,您是否可以使用单个 属性 来存储这些对象的数组?