https://developer.mozilla.org/ja/docs/Web/JavaScript
まずはここを見る。
prototypeプロパティconstructorプロパティすべてのオブジェクトインスタンスは、インスタンスを生成したコンストラクタ関数のprototypeにリンクする秘密のプロパティ(__proto__)を持っています。この秘密のリンクを使ってインスタンスのコンストラクタ関数のprototypeプロパティを取得できます。
開眼!JavaScript 042
※ 「すべてのオブジェクトインスタンス」は、コンストラクタも含む。
// []は配列のリテラルコンストラクタ関数
const a = ['a', 'b'];
// __proto__は非推奨なので、かわりにObject.getPrototypeOf()を使用
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
Array.prototype == Object.getPrototypeOf(a); // true
// クラスもカスタムコンストラクタ関数の糖衣構文
class Foo {};
const foo = new Foo();
Foo.protyotype === Object.getPrototypeOf(foo); // true
8.3 prototypeプロパティはすべてのFunction()インスタンスに自動的に付与される
開眼!JavaScript p111
ArrayやNumber)は、Function()のインスタンスfunction Hoge() { /* コード */}は、Function()のインスタンスコンストラクタ関数はprototypeプロパティを持つ// カスタムコンストラクタ関数
function Hoge() {};
Hoge.constructor === Function; // true
typeof Hoge.prototype; // object
// クラス
class Foo {};
Foo.constructor === Function; // true
typeof Foo.prototype; // Object
そしてFunction()のインスタンスで自動的に付与されたprotypeプロパティの__proto__にはObjectが設定されている。
コンストラクタを使って作成されたインスタンスには、オブジェクトを生成したコンストラクタ関数にポイントされたconstructorプロパティを持つ。
ref. 開眼!JavaScript 28
// 配列インスタンス
['a', 'b'].constructor == Array; // true
Array.constructor === Function; // true
// クラス
class Foo {};
const foo = new Foo();
foo.constructor === Foo;
Foo.constructor === Function;
| 型 | リテラル | コンストラクタ関数 |
|---|---|---|
| オブジェクト | {} |
Object |
| 数値 | 123 |
Number |
| 真偽値 | true or false | Boolean |
| 文字列 | 'foo' |
String |
| 配列 | [] |
Array |
| 関数 | なし | Function |