isPrototypeOf

  • Ciąg znaków tej wersji: Dokumentacja_języka_JavaScript_1.5/Obiekty/Object/isPrototypeOf
  • Tytuł wersji: isPrototypeOf
  • Identyfikator wersji: 281755
  • Utworzono:
  • Autor: Mgjbot
  • Czy jest aktualna? Nie
  • Komentarz robot dodaje: [[ja:Core JavaScript 1.5 Reference:Global Objects:Object:isPrototypeOf]]

Zawartość wersji

{{template.Dopracuj()}} 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( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf" } ) }}

Źródło wersji

<p>
{{template.Dopracuj()}}
<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( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/isPrototypeOf" } ) }}
Przywróć do tej wersji