How to avoid the "this." Prefixes before variables in the class if there are a lot of them? Example:
class Person { constructor() { this.name = "a"; this.lastName = "b"; this.age = 20; this.hair = "black"; } } How to avoid the "this." Prefixes before variables in the class if there are a lot of them? Example:
class Person { constructor() { this.name = "a"; this.lastName = "b"; this.age = 20; this.hair = "black"; } } Write all the properties first in the auxiliary object, and then assign them to this.
class Person { constructor() { let props = { name : "a", lastName : "b", age : 20, hair : "black" } for (let prop in props) { this[prop] = props[prop]; } } } But this is all nonsense, it will only make the code harder. Do not be afraid to use this, or use defineProperties, but the code will make it even thicker.
There is also an experimental Object.assing method.
Object.assing(this, { name : "a", lastName : "b", age : 20, hair : "black" }); But as far as I know it is supported only by Firefox, and for other browsers it exists only in the form of a polyfile or transplayer and in fact is an encapsulation of the method that I mentioned at the beginning.
x.age ? - GrundySource: https://ru.stackoverflow.com/questions/510932/
All Articles