LoginSignup
0
0

More than 1 year has passed since last update.

instanceof

Posted at

instanceof

どのコンストラクターから生成されたオブジェクトかを確認する。

コンストラクターから生成されたオブジェクトは、インスタンスというので、instanceofを使うことで確認できる。

function F(a, b) {
    this.a = a;
    this.b = b;
    // return {a: 1};
}

F.prototype.c = function() {}

const instance = new F(1,2);
console.log(instance instanceof F);

出力結果は true >> instanceはFから作られた → trueが出力される

function F(a, b) {
    this.a = a;
    this.b = b;
    return {a: 1};
}

F.prototype.c = function() {}

const instance = new F(1,2);

console.log(instance instanceof F);

出力結果は false >> instanceはFから作られたものじゃありません
参考

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