0
0

More than 3 years have passed since last update.

JavaScript, TypeScript 基本文法

Last updated at Posted at 2020-09-30

JavaScript

基礎

  • 関数も、値(オブジェクト)である
  • thisは、そのオブジェクト自身を表す
var obj = {
  name: "itoo",
  getname: function() {
    return this.name;
  },
  tel: "000-1111-2222"
};

アロー関数

function hello(name) {
  alert("hello, " + name()); // 備考:カッコ付きのため、nameは関数でないといけない
}

hello( () => { return "itoo"; } ); // アロー関数

関数(return ...)を引数にして、関数(hello())を使う。

get(set)

get(set)が冒頭のついているものは、ゲッター(セッター)。
プロパティのように値を取り出す(セットする)。

class Test {
  get getScore() { ... }
}

test.getScore // test.getScore() じゃない

Typescript

  • リテラル型を、定数のように使用しないこと。

配列

const counters: number[] = [0, 1, 2, 3];

オブジェクト

let profile: { name: string } = { name: "itoo" };

関数

// 1.普通の関数
function sum1(a: number, b: number): number {
  return a + b;
}
// 2.無名関数
const sum2 = function (a: number, b: number): number {
  return a + b;
};
// 3.アロー関数
const sum3 = (a: number, b: number): number => {
  return a + b;
};
// 4. 3の1行ver.
const sum4 = (a: number, b: number): number => a + b; // returnを書かないこと

クラス

class Person {
  // name: string;
  // age: number;

  // constructor(name: string, age: number) {
  //   this.name = name;
  //   this.age = age;
  // }

  constructor(public name: string, public age: number) {}  // 上の記述と同一

  introduce(): string {
    return `${this.name} . ${this.age}`;
  }
}
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