Is the following an acceptable way to initialize a class?
class Person {
name = undefined; // or just `name` ?
age = undefined; // or just `age` ?
constructor(name, age) {
Object.assign(this, {name, age});
}
}
let p = new Person('tom', 10);
By that I mean, more specifically,
- Is
Object.assign(this, {...variables})
a good way to do all thethis
assignments? - Is it considered good practice to stick the variables at the top, or should they be placed within the constructor?
- Is it better to explicitly set a variable to
undefined
or not? That is, isname = undefined;
orname;
preferable?