mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>请你编写一个函数,检查给定的对象是否是给定类或超类的实例。</p>
 | 
						||
 | 
						||
<p>可以传递给函数的数据类型没有限制。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>func = () => checkIfInstance(new Date(), Date)
 | 
						||
<b>输出:</b>true
 | 
						||
<strong>解释:</strong>根据定义,Date 构造函数返回的对象是 Date 的一个实例。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
 | 
						||
<b>输出:</b>true
 | 
						||
<strong>解释:</strong>
 | 
						||
class Animal {};
 | 
						||
class Dog extends Animal {};
 | 
						||
checkIfInstance(new Dog(), Animal); // true
 | 
						||
 | 
						||
Dog 是 Animal 的子类。因此,Dog 对象同时是 Dog 和 Animal 的实例。</pre>
 | 
						||
 | 
						||
<p><strong>示例 3:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>func = () => checkIfInstance(Date, Date)
 | 
						||
<b>输出:</b>false
 | 
						||
<strong>解释:</strong>日期的构造函数在逻辑上不能是其自身的实例。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong>示例 4:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>func = () => checkIfInstance(5, Number)
 | 
						||
<b>输出:</b>true
 | 
						||
<strong>解释:</strong>5 是一个 Number。注意,"instanceof" 关键字将返回 false。</pre>
 |