Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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/9/ruby-on-rails-3/4.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
Typescript 将对象传递到一个函数中,并使用该对象中的键/值调用另一个函数_Typescript - Fatal编程技术网

Typescript 将对象传递到一个函数中,并使用该对象中的键/值调用另一个函数

Typescript 将对象传递到一个函数中,并使用该对象中的键/值调用另一个函数,typescript,Typescript,在python中,我通常只执行以下操作: ## single person class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age ## empty when instantiated class People: def _

在python中,我通常只执行以下操作:

## single person
class Person:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

## empty when instantiated
class People:
    def __init_(self):
        self._people = {}

    def update(self, Person):
        self._people[Person.first_name] = Person

    def update2(self, person):
        self.update(Person(**person))


people = People()
people.update2({'first_name': 'Mike', 'last_name': 'Smith', 'age':7})
我的目标是在typescript中实现这个精确的行为。这是我到目前为止所拥有的

class  Person {
    constructor(first_name, last_name, age){}
}

class People{
    public _people;

    constructor(){
        //not sure if there is a cleaner way to add _people to new instances
        this._people = {}
    }

    update(Person){
        this._people[Person.first_name] = Person
    }
    update2(my_object){
        //Person(my_object) should return an instance of the Person class
        this.update(Person(my_object))
    }
}

var people = new People()
people.update2({first_name:'Bob', last_name:'Smith', age:7})
对非python人员的解释。

目标是创建一个可以保存Person类实例的People类。我想将一个对象传递到update2中,并使用该对象的键/值创建Person类的实例


如果有什么不清楚的,请告诉我

以下是我将如何用打字脚本编写它。我改变了一些变量的大小写,使其更像“TypeScript”

我还添加了缺少的类型

class Person {
    // Public getters/setters
    public firstName: string;
    public lastName: string;
    public age: number;

    constructor({firstName, lastName, age}: { firstName?: string, lastName?: string, age?: number }) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

class People {
    // Declare _people as an object with keys as strings and values as Person instances
    public _people: {[key: string]: Person};

    update(person: Person) {
        this._people[person.firstName] = person;
    }

    // Add a signature to obj to make it clear what you are expecting
    update2(obj: {firstName: string, lastName: string, age: number}) {
        this.update(new Person(obj));
    }
}

var people = new People()
people.update2({ firstName: 'Bob', lastName: 'Smith', age: 7 });

console.log(people._people);
或者,如果
Person
是一个简单的数据对象,我建议您不要使用
类,而是使用一个带有接口的简单JS对象:

interface Person {
    firstName?: string,
    lastName?: string,
    age?: number
}

const person1: Person = { firstName: 'Jane', lastName: 'Doe', age: 25};

几个问题。就个人而言,构造函数中的代码不是自动完成的,而不需要写出它吗?在People中,是否会在所有People实例中共享人员?我希望每个新的人物实例都有自己的人物对象。而且,在很多情况下,我的类有很多属性。在update2中是否有避免写入obj.firstName、obj.lastName等的方法?谢谢,这可能正是我想要的。编辑了我的答案。不,据我所知,您需要在构造函数中设置属性<代码>\u人
不会在实例之间共享(就像我所知道的任何OO语言一样)。为了避免编写obj.firstName等,可以使用对象分解,如我现在所示。如果Person是一个简单的数据对象,只需将对象直接与接口而不是类一起使用即可。(在进行分解时,似乎需要对其进行设置,否则不需要)。在python中,类变量在实例之间共享。人会有很多方法,在实际执行中。谢谢你的小片段,应该能带我到我需要的地方。