mozilla

Revision 43376 of Object.prototype.isPrototypeOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/isPrototypeOf
  • Revision title: isPrototypeOf
  • Revision id: 43376
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[ja:Core JavaScript 1.5 Reference:Global Objects:Object:isPrototypeOf]]

Revision Content

isPrototypeOf allows you to check whether or not an object instance has a class in question as one of its parents.

For example, the following code creates a class Question and another class SecondaryQuestion that extends Question:

function Question {
  this.answer = 42;
}

function SecondaryQuestion {

}
SecondaryQuestion.prototype = new Question();

Later on down the road, if you instantiate SecondaryQuestion and need to check if it has Question as a parent class, you could do this:

var myquestion = new SecondaryQuestion();

...

if (Question.prototype.isPrototypeOf(myquestion)) {
  // do something with the question
}

This particularly comes in handy if you have a function or class method that can only accept an instance of a certain class as its parameter:

function guessAnswer(q) {
  if (Question.prototype.isPrototypeOf(q)) {
    // Try to answer the question.
    // This doesn't care if you've passed a Question or a
    // SecondaryQuestion, since they're both Questions.
  } else {
    // Get upset
  }
}
{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/isPrototypeOf" } ) }}

Revision Source

<p>
</p><p><code>isPrototypeOf</code> allows you to check whether or not an object instance has a class in question as one of its parents.
</p><p>For example, the following code creates a class <code>Question</code> and another class <code>SecondaryQuestion</code> that extends <code>Question</code>:
</p>
<pre class="eval">function Question {
  this.answer = 42;
}

function SecondaryQuestion {

}
SecondaryQuestion.prototype = new Question();
</pre>
<p>Later on down the road, if you instantiate <code>SecondaryQuestion</code> and need to check if it has <code>Question</code> as a parent class, you could do this:
</p>
<pre class="eval">var myquestion = new SecondaryQuestion();

...

if (Question.prototype.isPrototypeOf(myquestion)) {
  // do something with the question
}
</pre>
<p>This particularly comes in handy if you have a function or class method that can only accept an instance of a certain class as its parameter:
</p>
<pre class="eval">function guessAnswer(q) {
  if (Question.prototype.isPrototypeOf(q)) {
    // Try to answer the question.
    // This doesn't care if you've passed a Question or a
    // SecondaryQuestion, since they're both Questions.
  } else {
    // Get upset
  }
}
</pre>{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/isPrototypeOf" } ) }}
Revert to this revision