I have the following typescript class. In the constructor, does it matter whether I…
- User
I have the following typescript class. In the constructor, does it matter whether I access the _eventBus class attribute by directly accessing the parameter
\_eventBus, compared to reading it likethis.\_eventBus?codeclass Foo { constructor( private \_eventBus: object, ) { const foo = \_eventBus; const foo2 = this.\_eventBus; } } - Assistant
In the constructor of the
Fooclass, both ways of accessing the_eventBusattribute, directly using the parameter_eventBusand accessing it throughthis._eventBus, will give you the same value. However, it is a common convention in TypeScript to usethis._eventBusto access class attributes, rather than directly accessing the parameter_eventBus.Using
this._eventBusmakes it clear that you are accessing an attribute of the class, rather than just a variable that happens to have the same name. It can also make the code more consistent with other parts of the class that access the attribute.So, while it does not technically matter which way you access
_eventBusin the constructor, usingthis._eventBusis the convention in TypeScript and can make your code more clear and consistent. - User
I read that accessing it directly using the parameter can help with code minification. Do you know anything about that?
- Assistant
Yes, you are correct that accessing class attributes directly using their constructor parameters can help with code minification.
When TypeScript code is compiled to JavaScript, class properties are transpiled to a combination of a constructor function and prototype methods. In order to minimize the size of the transpiled code, TypeScript can omit the creation of a separate property for each class attribute and instead access them directly through their constructor parameters. This technique is called "parameter properties".
When using parameter properties, the constructor parameter is automatically declared as a class attribute, so you can use it just like any other class attribute. This allows you to access the attribute directly using its constructor parameter, without the need to declare an explicit property.
In your example, using parameter properties, you can declare the
_eventBusattribute as follows:kotlinclass Foo { constructor(private readonly _eventBus: object) { const foo = this._eventBus; } } - User
does the usage of that parameter property within the constructor, affect minification?
\_eventBusvsthis.\_eventBus? - Assistant
Using a parameter property in the constructor, such as
_eventBus, rather than explicitly declaring the property as a class attribute, such asthis._eventBus, can make a difference in the size of the minified code.When using a parameter property, the TypeScript compiler will generate JavaScript code that accesses the
_eventBusattribute directly through the constructor parameter, without generating an explicit property. This can result in smaller code because the generated code does not need to include the extra property declaration and property accessors.For example, if you have the following TypeScript class: