Coming from Java it feels more natural to write a class this way:
class Person1 {
private name: string;
constructor(name: string) {
this.name = name;
}
getName(): string {
return this.name;
}
}
According to http://www.typescriptlang.org/play/ this gets transpiled to fairly easy ES5 code:
var Person1 = /** @class */ (function () {
function Person1(name) {
this.name = name;
}
Person1.prototype.getName = function () {
return this.name;
};
return Person1;
}());
However, the official documentation uses the keyword get
& set
like this:
class Person2 {
private _name: string;
constructor(name: string) {
this._name = name;
}
get name(): string {
return this._name;
}
}
Which gets transpiled to this:
var Person2 = /** @class */ (function () {
function Person2(name) {
this._name = name;
}
Object.defineProperty(Person2.prototype, "name", {
get: function () {
return this._name;
},
enumerable: true,
configurable: true
});
return Person2;
}());
Are there technical advantages in the resulting ES5 code?
Thanks!
1) getter and setter method gives you centralized control on how a the particular field is initialized and provided to the client which makes the validation and debugging much easier. you can simply put breakpoints or print statement to see which thread are accessing and what values are going out.
The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement. 2.
An accessor method allows other objects to obtain the value of instance variables or static variables. A non-void method returns a single value. Its header includes the return type in place of the keyword void. Accessor methods that return primitive types use “return by value” where a copy of the value is returned.
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.
This question isn't really a good fit for stackoverflow as any answers you get are bound to be largely based on opinion, however:
There are several reasons why public attributes are preferred over getter and setter methods, these include the cleaner syntax, and also the reduced boilerplate as you don't have to declare getter and setter methods. Also, in Javascript or Typescript you are likely to be using plain objects quite often, so for example:
type IPerson = {
name: string
};
class Person implements IPerson {
constructor (public name: string) {};
}
function greet(person: IPerson) {
console.log(`Hello ${person.name}`);
}
greet(new Person('Napoleon'));
greet({name: 'Snowball'});
Now if you actually need to do something fancy when the attribute is accessed (or updated), you can have a class with get
/set
on the attribute:
class Person2 implements IPerson {
constructor(private _firstname: string, private _lastname: string) {
}
get name(): string {
return `${this._firstname} ${this._lastname}`;
}
}
greet(new Person2('Old', 'Major'))
But the important thing to remember is that you would not define get
and set
if the only thing they do is access a private attribute, there's just no reason to do that.
Using getters and setters is only prevalent in languages like Java where it is the only option to protect access to attributes on objects. In other languages which support get
and set
properties it is much more common to simply expose attributes as public. Then if required you can change the class definitions later to use get
and set
, but only if you need it. Changing the implementation this way does not require any change to the code which uses the class.
This is the important distinction: in Java if you need to change from a public attribute to a computed property you would have to change the declared interface affecting everything that uses the class, so you avoid ever exposing attributes as public. In typescript and most other languages only the class implementation changes, not the exposed interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With