LoginSignup
2
2

More than 5 years have passed since last update.

学習記録:javascript(ES6)

Last updated at Posted at 2018-08-27

8月27日 javascript(ES6)

個人的に重要な部分のメモ
コードはprogate内の問題より引用

 console.log(`${this.age}歳です`);

ES6では文字列内で変数や定数を用いる際に、+で文を繋がず
${変数や定数} のテンプレートリテラルを使用する事が出来る
その際は、(””)ではなく、(``)のバッククォートで囲む

for (let i = 0 ; i < cafe.menus.length ; i ++ ) {
  console.log(cafe.menus[i]) ;
} 

for (変数の設定 ; 条件式 ; 変数に対する処理) {
 実行する処理 }
※最後に ; は付けないので注意

const convertToYen = (priceDollar) => {
  return priceDollar * dollarYenRate ;
} ;

const 変数名 = (引数名) => {
 実行する処理
} ;
※() => はアロー関数であり、function()と同義

class Animal {
  //コンストラクタ
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  //メソッド1
  greet() {
    console.log("こんにちは");
  }
  //メソッド2
  info() {
    this.greet() ;
    console.log(`名前は${this.name}です`);
    console.log(`${this.age}歳です`);
  }
}
// インスタンスの生成文
const animal = new Animal("レオ", 3);
// 実行文
animal.info();

インスタンスの生成 ⇒ const インスタンス名 = new クラス名
インスタンスでanimalクラスのメソッドを使用する ⇒ インスタンス名.info

//分割したファイルの読み込み
import Animal from "./animal.js" ;

//分割したファイルを他のファイルでも使用可能にする
export default Animal ;

分割したファイルの読み込み 
⇒ import クラス名 from "./ファイル名" ;
分割したファイルを他のファイルでも使用可能にする
⇒ export default クラス名 ;

//animal.js
class Animal {
  //コンストラクタの設計
  constructor(name, age) {
    this.name = name ; this.age = age;
  }
}

//dog.js
//Animalクラス(親)からDogクラス(子)へ継承
class Dog extends Animal {
    //親クラスのコンストラクタのオーバーライド
    constructor(name, age, breed) {
      //親クラスのコンストラクタの呼び出しと新しいコンストラクタの追加
      super(name , age) ;
      this.breed = breed ;
    }
}

子クラスのコンストラクタ内で、親クラスのコンストラクタを呼び出す
⇒ super(引数名,…)」

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