Reputation: 101
I'm sure this is simple code, but I run into weird error but has no answer to. I did also look up the answer from this: get and set in TypeScript
But my code is exact same thing. But I have this error when I set the instance's property:
This expression is not callable.
Type 'String' has no call signatures
This is my code
/**
* Class that use underscore name and get set syntax
*/
class Person {
// can give any property name, use convention `_`
private _fname: string;
private _lname: string;
constructor(first: string, last: string) {
this._fname = first;
this._lname = last;
}
public get firstname(): string {
return this._fname;
}
public set firstname(first: string) {
this._fname = first;
}
public get lastname(): string {
return this._lname;
}
public set lastname(last: string) {
this._lname = last;
}
}
// create instance
let character = new Person("Laila", "Law-Giver");
console.log(`Jarl of Riften is ${character.firstname} ${character.lastname}`);
character.firstname("Saerlund");
console.log(`${character.firstname} ${character.lastname} is her son, who sides with the Empire.`);
I have error on character.firstname("Saerlund");
What does this mean? I don't see what is wrong with the code.
Upvotes: 1
Views: 4415
Reputation: 426
You declared firstname
as a setter and in order to assign a new value to it you should do as follows:
character.firstname = "Saerlund";
Upvotes: 5