[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf [Back]  [Original]

Object.prototype.isPrototypeOf() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.prototype.isPrototypeOf()

Baseline

20157

isPrototypeOf() Object

: isPrototypeOf() instanceof object instanceof AFunction object AFunction AFunction.prototype

function Foo() {}
function Bar() {}

Bar.prototype = Object.create(Foo.prototype);

const bar = new Bar();

console.log(Foo.prototype.isPrototypeOf(bar));
// : true
console.log(Bar.prototype.isPrototypeOf(bar));
// : true

js
isPrototypeOf(object)

object

(this) object this false

TypeError

this null undefined

Object.prototype null isPrototypeOf() object false this object this

isPrototypeOf

baz Baz.prototype Bar.prototypeFoo.prototypeObject.prototype

js
class Foo {}
class Bar extends Foo {}
class Baz extends Bar {}

const foo = new Foo();
const bar = new Bar();
const baz = new Baz();

// :
// foo: Foo --> Object
// bar: Bar --> Foo --> Object
// baz: Baz --> Bar --> Foo --> Object
console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Baz.prototype.isPrototypeOf(bar)); // false
console.log(Baz.prototype.isPrototypeOf(foo)); // false
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(foo)); // false
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(bar)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true

isPrototypeOf() instanceof

baz Foo.prototype

js
if (Foo.prototype.isPrototypeOf(baz)) {
  // do something safe
}

Foo.prototype baz baz Foo baz Foo.prototype baz Foo

js
class Foo {
  #value = "foo";
  static getValue(x) {
    return x.#value;
  }
}

const baz = { __proto__: Foo.prototype };

if (Foo.prototype.isPrototypeOf(baz)) {
  console.log(Foo.getValue(baz)); // TypeError:  #value 
}

instanceof in

js
class Foo {
  #value = "foo";
  static getValue(x) {
    return x.#value;
  }
  static isFoo(x) {
    return #value in x;
  }
}

const baz = { __proto__: Foo.prototype };

if (Foo.isFoo(baz)) {
  // baz  Foo 
  console.log(Foo.getValue(baz));
}

ECMAScript 2027 LanguageSpecification
# sec-object.prototype.isprototypeof


Web Proxy Viewer  |  New URL  |  Original Page