1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-25 17:50:26 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/检查是否是类的对象实例 [check-if-object-instance-of-class].html

41 lines
1.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>请你编写一个函数,检查给定的值是否是给定类或超类的实例。</p>
<p>可以传递给函数的数据类型没有限制。例如,值或类可能是&nbsp; <code>undefined</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>func = () =&gt; checkIfInstance(new Date(), Date)
<b>输出:</b>true
<strong>解释:</strong>根据定义Date 构造函数返回的对象是 Date 的一个实例。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>func = () =&gt; { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
<b>输出:</b>true
<strong>解释:</strong>
class Animal {};
class Dog extends Animal {};
checkIfInstanceOf(new Dog(), Animal); // true
Dog 是 Animal 的子类。因此Dog 对象同时是 Dog 和 Animal 的实例。</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>func = () =&gt; checkIfInstance(Date, Date)
<b>输出:</b>false
<strong>解释:</strong>日期的构造函数在逻辑上不能是其自身的实例。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<b>输入:</b>func = () =&gt; checkIfInstance(5, Number)
<b>输出:</b>true
<strong>解释:</strong>5 是一个 Number。注意"instanceof" 关键字将返回 false。</pre>