1
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?

コンストラクタ関数

Last updated at Posted at 2025-05-12

コンストラクタ関数とは

 ・主にビルトインオブジェクトで使われる
 ・新しくオブジェクトを生成できる関数
  =この新しいオブジェクトのことをインスタンスと呼ぶ(要はコピー)
 →コンストラクタ関数が雛形となり、関数を再度書く必要がなくなる
 ・ES6以降はclassが登場、async,awaitへ

[ コンストラクタ関数の作り方 ]

function Obj(){      //※コンストラクタ関数は頭文字を大文字にする
   this.name = '太郎';
}
let a = new Obj();     //new演算子によってインスタンスを作成する
console.log( a.name );   // '太郎'

・変数 a は、コンストラクタ関数(Obj)のプロパティを引き継ぐ
・コンストラクタ関数とは別のオブジェクトとして認識、だから便利

つまり、Obj != a であるが、値は保持され機能を変更だけ変更可能。
よって、コードの構造化、再利用性、保守性というメリットが得られる。

 非同期処理で使われるPromiseはビルトインオブジェクトのコンストラクタ関数であるため、この概念の理解が重要となる。
  ES6以降では、classやasync,awaitがこの性質を受け継ぐため、必ず理解しておくべき。

1
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
1
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?