mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
update
This commit is contained in:
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
40
leetcode/problem/check-if-object-instance-of-class.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>Write a function that checks if a given object is an instance of a given class or superclass.</p>
|
||||
|
||||
<p>There are no constraints on the data types that can be passed to the function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(new Date(), Date)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
class Animal {};
|
||||
class Dog extends Animal {};
|
||||
checkIfInstance(new Dog(), Animal); // true
|
||||
|
||||
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(Date, Date)
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => checkIfInstance(5, Number)
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation: </strong>5 is a Number. Note that the "instanceof" keyword would return false.
|
||||
</pre>
|
Reference in New Issue
Block a user