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.

【javascript】Class

Posted at

case:クラスに書き換える。

  • メリット
    • メソッドの追加が簡単
    • 簡潔に書ける

function Person(name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.hello = function() {
  console.log('hello ' + this.name);
}

classに書き換え

//クラスを定義

class Person {
  constructor(name, age){ //コンストラクタを用意
    this.name = name;
    this.age = age;
  }

  hello(){ //メソッドの追加 > 今まではprototypeで追加していた。
    console.log('hello' + this.name)
  }
}

//呼び出し方は同じ。
const bob = new Person('bob', 22)
console.log(bob) 
>> Person {name: 'bob', age: 22}
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?