この翻訳は不完全です。英語から この記事を翻訳 してください。
概要
オブジェクトが別のオブジェクトのプロトタイプチェーンに存在するかどうかを判定します。
構文
prototype.isPrototypeOf(object)
引数
- prototype
- 引数に指定したオブジェクトのプロトタイプチェーン内の各リンクに対してテストするオブジェクト
注記:
isPrototypeOfは instanceof 演算子 とは異なるものです。In the expressionobject instanceof AFunction, theobjectprototype chain is checked againstAFunction.prototype, not againstAFunctionitself - object
- プロトタイプチェーンの検索対象
説明
isPrototypeOf を用いると、オブジェクトが別のオブジェクト内のプロトタイプチェーンに存在するかどうかをチェックする事ができます。
例えば、次の様なプロトタイプチェーンがあったとします。
function Fee() {
// . . .
}
function Fi() {
// . . .
}
Fi.prototype = new Fee();
function Fo() {
// . . .
}
Fo.prototype = new Fi();
function Fum() {
// . . .
}
Fum.prototype = new Fo();
Later on down the road, if you instantiate Fum and need to check if Fi's prototype exists within the Fum prototype chain, you could do this:
var fum = new Fum();
. . .
if (Fi.prototype.isPrototypeOf(fum)) {
// do something safe
}
This, along with the instanceof operator particularly comes in handy if you have code that can only function when dealing with objects descended from a specific prototype chain, e.g., to guarantee that certain methods or properties will be present on that object.