LeetCode JavaScript 30 Days Challenge – Day25 – 2618. Check if Object Instance of Class

    LeetCode JavaScript 30 Days Challenge

    Write a function that checks if a given value is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class’s methods.
    There are no constraints on the data types that can be passed to the function. For example, the value or the class could be undefined.

    寫一個函式來檢查,確認給予的數值是否為一個 class 或 superclass。
    對於這個問題,一個陣列被考慮為 class,是因為這個物件可以進入到 class's methods。
    對於可以傳遞給函式的數據類型上沒有任何限制。例如,值或類別可以是未定義的。
    

    Example 1:

    Input: func = () => checkIfInstanceOf(new Date(), Date)
    Output: true
    

    Example 2:

    Input: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
    Output: true
    

    Example 3:

    Input: func = () => checkIfInstanceOf(Date, Date)
    Output: false
    

    Example 4:

    Input: func = () => checkIfInstanceOf(5, Number)
    Output: true
    

    solution:
    建立 checkIfInstanceOf 函式來確認題目給的 obj 是否存在 classFunction 中。
    首先我們先檢查 obj 是否為 null 或 undefined,如果是的話,回傳false,因為這兩個型態不會存在 class 中。
    接著,我們檢查 classFunction 是否為函式的類型,如果不是的話,回傳 false,以此確保傳入的是有效的函式。
    如果 obj 和 classFunction 都成功通過了兩個判斷式,那就使用 Object 來將 obj 轉成物件,
    以便使用 instanceof 來檢查是否來自 classFunction。

    Code 1: BigO(1)

    var checkIfInstanceOf = function(obj, classFunction) {
        if (obj === null || obj === undefined) return false;
        if (typeof classFunction !== "function") return false;
        return Object(obj) instanceof classFunction;
    };
    

    FlowChart:
    Example 1

    var func = () => checkIfInstanceOf(new Date(), Date)
    console.log(typeof obj, typeof classFunction) //object function
    console.log(func()); // true
    

    Example 2

    var func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
    console.log(typeof obj, typeof classFunction) //object function
    console.log(func()); //true
    

    Example 3

    var func = () => checkIfInstanceOf(Date, Date)
    console.log(typeof obj, typeof classFunction) //function function
    console.log(func()); // false
    

    Example 4

    var func = () => checkIfInstanceOf(5, Number)
    console.log(typeof obj, typeof classFunction) //number function 
    console.log(func()); //true
    

    Other Example
    Example 5
    var func = () => checkIfInstanceOf(0, null)
    console.log(typeof obj, typeof classFunction) //number function
    console.log(func()); // false

    var func = () => checkIfInstanceOf(null, null)
    console.log(typeof obj, typeof classFunction) //object function
    console.log(func()); // false

    var func = () => checkIfInstanceOf([], null)
    console.log(typeof obj, typeof classFunction) //object function
    console.log(func()); // false

    var func = () => checkIfInstanceOf([], Object)
    console.log(typeof obj, typeof classFunction) //object function
    console.log(func()); // true