In python I usually just do something like this:
## 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})
My goal is to implement that exact behavior in typescript. Here is what I have so far.
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})
Explanation for non-python people.
The goal is to create a People class that can hold instances of the Person class. I want to pass an object into update2, and use the keys/values of that object to create instances of the Person class.
Please let me know if anything isn't clear.