Skip to content

Latest commit

 

History

History

2618.Check if Object Instance of Class

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

English Version

题目描述

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

可以传递给函数的数据类型没有限制。例如,值或类可能是  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。

解法

TypeScript

function checkIfInstanceOf(obj: any, classFunction: any): boolean {
    if (classFunction === null || classFunction === undefined) {
        return false;
    }
    while (obj !== null && obj !== undefined) {
        const proto = Object.getPrototypeOf(obj);
        if (proto === classFunction.prototype) {
            return true;
        }
        obj = proto;
    }
    return false;
}

/**
 * checkIfInstanceOf(new Date(), Date); // true
 */

...