LoginSignup
5
2

More than 5 years have passed since last update.

Javascriptのclassについて

Last updated at Posted at 2018-04-25

classについて

・ECMAScript2015から採用。
・classは設計図。
・classにより、Javascriptでもオブジェクト指向プログラミングが簡潔に書けるようになった。

class Person {
  constructor(name) {
    this.name = name;
  }
  shokai() {
    console.log("初めまして" + this.name + "です。");
  }
}
var ayana = new Person("ayana");
ayana.shokai();


//=> 初めましてayanaです。

上記のコードを例にみていくと、
①classキーワードでPersonというクラスを定義。
②次にconstructorメソッドとshokaiというメソッドを定義。(constructorメソッドはclassをnewして作ったインスタンス生成時に呼ばれる。)
※this.nameのthisは生成するオブジェクト(Personインスタンス)を参照している。

5
2
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
5
2