0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TYpescript のsetter , getter 。初心者の私が躓いたところ。

0
Posted at

プログラマーとして働き始めて早3か月近くがたつ。
自分の実力がなかなか、向上する兆しがみえないので、ここでアウトプットしていくことにした。

【参考にさせていただいたQiitaの記事など】

  1. https://qiita.com/ringtail003/items/7ccf992f18b768e0e633

  2. https://qiita.com/suin/items/7d6837a0342b36891099

今日はまず、setter とgetter の使い方について、
言わずモノが、情報をセットして、セットしたところから情報を取り出すというイメージ。
【setter,getter】

class Person {
    firstName!: string
    lastName!: string

    set fullName(name:string){
        let names = name.split(' ');
        this.firstName = names[1];
        this.lastName = names[0];
    }
    get fullName():string{
        return this.lastName +this.firstName;
    }
}
let p:Person;
p = new Person();
p.fullName='田中 太郎';
console.log(p.fullName);//田中太郎

ここで私がハマったところだが、
”田中太郎undefined”とコンソール上に表示されてしまった。なんで!!
splitメソッドの''のスペースが全角スペースではなく、半角スペースだったのだ。
初心者なので、仕方がない(笑)

今日の勉強はここまで★

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?