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 1 year has passed since last update.

そうじゃないだろう@災い転じて福となす

Last updated at Posted at 2023-09-28

災い転じて福となす

まとめ
const { 服, 茄子 } = 災い.転じる()がしたかっただけ

index.ts
import {  as 服クラス } from "./服";
import { 茄子 as 茄子クラス } from "./茄子";

class わざわい {
  : 服クラス;
  茄子: 茄子クラス;
  constructor() {
    this. = new 服クラス();
    this.茄子 = new 茄子クラス();
  }

  public 転じる() {
    return {
      : this.,
      茄子: this.茄子,
    };
  }
}

const 災い = new わざわい();

const { , 茄子 } = 災い.転じる();

.眺める();

.着る();

茄子.焼く();

茄子.食べる();

.脱ぐ();

茄子.眺める();

服.ts
enum 服のサイズ {
  XS = 1,
  S = 2,
  M = 3,
  L = 4,
  XL = 5,
}

enum 服の色 {
  RED = 1,
  BLUE = 2,
  YELLOW = 3,
  GREEN = 4,
  BLACK = 5,
  WHITE = 6,
  PINK = 7,
  PURPLE = 8,
  ORANGE = 9,
  BROWN = 10,
}
function 色ゲットだぜ(value: number | undefined): string | undefined {
  for (const [key, val] of Object.entries(服の色)) {
    if (val === value) {
      return key;
    }
  }
  return undefined;
}

function サイズゲットだぜ(value: number | undefined): string | undefined {
  for (const [key, val] of Object.entries(服のサイズ)) {
    if (val === value) {
      return key;
    }
  }
  return undefined;
}

export class  {
  サイズ: 服のサイズ | undefined; // undefinedはフリーサイズ
  : 服の色 | undefined; // undefinedは透明
  constructor() {
    // 服のサイズと色はランダム
    this.サイズ = Math.floor(Math.random() * 5) + 1;
    this. = Math.floor(Math.random() * 10) + 1;
  }

  public 着る(): void {
    console.log("服を着た");
  }
  public 脱ぐ(): void {
    console.log("服を脱いだ");
  }
  public 眺める(): void {
    console.log("わぁ! 服だ!");
    console.log(
      `サイズ: ${サイズゲットだぜ(this.サイズ)}, 色: ${色ゲットだぜ(this.)}`
    );
    console.log();
  }
}

茄子.ts
enum 長さ {
  めっちゃ短い = 0,
  短い = 1,
  普通 = 2,
  長い = 3,
  めっちゃ長い = 4,
  果てしなく長い = 5
}

function 長さゲットだぜ(value: number | undefined): string | undefined {
  for (const [key, val] of Object.entries(長さ)) {
    if (val === value) {
      return key;
    }
  }
  return undefined;
}

enum 太さ {
  めっちゃ細い,
  細い,
  普通,
  太い,
  ごりごりに太い,
  とんでもなく太い
}

function 太さゲットだぜ(value: number | undefined): string | undefined {
  for (const [key, val] of Object.entries(太さ)) {
    if (val === value) {
      return key;
    }
  }
  return undefined;
}

export class 茄子 {
  長さ: 長さ;
  太さ: 太さ;
  constructor() {
    // 茄子の長さと太さはランダム。果てしなく長い、とんでもなく太いは3%の確率で出現
    if (this.isRare()) {
      this.長さ = 長さ.果てしなく長い;
    } else {
      this.長さ = Math.floor(Math.random() * 5);
    }
    if (this.isRare()) {
      this.太さ = 太さ.とんでもなく太い;
    } else {
      this.太さ = Math.floor(Math.random() * 5);
    }
  }
  private isRare(): boolean {
    return Math.random() < 0.03;
  }

  public 焼く(): void {
    console.log("茄子を焼いた");
  }
  public 食べる(): void {
    console.log("茄子を食べた");
  }
  public 眺める(): void {
    console.log("これは茄子ですね。")
    // どちらかがレアの場合、レアな表示にする。
    // どちらもレアの場合、すっごくレアな表示にする。
    // すっごくレア
    if (this.長さ === 長さ.果てしなく長い && this.太さ === 太さ.とんでもなく太い) {
      console.log("激レア茄子");
      return;
    }
    // どちらかがレア
    if (this.長さ === 長さ.果てしなく長い || this.太さ === 太さ.とんでもなく太い) {
      console.log("レア茄子");
      return;
    }
    
    console.log(`長さ: ${長さゲットだぜ(this.長さ)}, 太さ: ${太さゲットだぜ(this.太さ)}`);
  }
}

激レア茄子を引けるように頑張ろう

ここからは補足

さすがにひどいかもしれないので、この記事のクライマックスである下記の動作を説明しよう(説明しようっ(キリッ))

const { , 茄子 } = 災い.転じる();

これは、下記みたいなことを1行でかけてしまうすごい書き方

const result = 災い.転じる();
const  = result.
const 茄子 = result.茄子

すっきりするね!

やりすぎると意味わかんなくなるけど。
ネストもいけるし、エイリアスもできるし。超かっこいい!!

const objA = {
    nickName: "Taro",
    age: {
        forPrivate: 22,
        forPublic: 16
    }
}

const {
    nickName: name,
    age: {
        forPublic: age
    }
} = objA

console.log(name) // Taro
console.log(age) // 16

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?