调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关企业
提示

请你编写一个函数,检查给定的值是否是给定类或超类的实例。

可以传递给函数的数据类型没有限制。例如,值或类可能是  undefined

 

示例 1:

输入:func = () => checkIfInstance(new Date(), Date)
输出:true
解释:根据定义,Date 构造函数返回的对象是 Date 的一个实例。

示例 2:

输入:func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
输出:true
解释:
class Animal {};
class Dog extends Animal {};
checkIfInstanceOf(new Dog(), Animal); // true

Dog 是 Animal 的子类。因此,Dog 对象同时是 Dog 和 Animal 的实例。

示例 3:

输入:func = () => checkIfInstance(Date, Date)
输出:false
解释:日期的构造函数在逻辑上不能是其自身的实例。

示例 4:

输入:func = () => checkIfInstance(5, Number)
输出:true
解释:5 是一个 Number。注意,"instanceof" 关键字将返回 false。
通过次数
8.5K
提交次数
24.7K
通过率
34.5%

相关企业

提示 1
In Javascript, inheritance is achieved with the prototype chain.

提示 2
You can get the prototype of an object with the Object.getPrototypeOf(obj) function. Alternatively, you can code obj['__proto__'].

提示 3
You can compare an object's __proto__ with classFunction.prototype.

提示 4
Traverse the entire prototype chain until you find a match.

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
func =
() => checkIfInstanceOf(new Date(), Date)
Source