{ "data": { "question": { "questionId": "2758", "questionFrontendId": "2618", "boundTopicId": null, "title": "Check if Object Instance of Class", "titleSlug": "check-if-object-instance-of-class", "content": "
Write a function that checks if a given object is an instance of a given class or superclass.
\n\nThere are no constraints on the data types that can be passed to the function.
\n\n\n
Example 1:
\n\n\nInput: func = () => checkIfInstance(new Date(), Date)\nOutput: true\nExplanation: The object returned by the Date constructor is, by definition, an instance of Date.\n\n\n
Example 2:
\n\n\nInput: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }\nOutput: true\nExplanation:\nclass Animal {};\nclass Dog extends Animal {};\ncheckIfInstance(new Dog(), Animal); // true\n\nDog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.\n\n
Example 3:
\n\n\nInput: func = () => checkIfInstance(Date, Date)\nOutput: false\nExplanation: A date constructor cannot logically be an instance of itself.\n\n\n
Example 4:
\n\n\nInput: func = () => checkIfInstance(5, Number)\nOutput: true\nExplanation: 5 is a Number. Note that the "instanceof" keyword would return false.\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 39, "dislikes": 10, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "() => checkIfInstanceOf(new Date(), Date)\n() => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }\n() => checkIfInstanceOf(Date, Date)\n() => checkIfInstanceOf(5, Number)", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Object} object\n * @param {Function} classFunction\n * @return {boolean}\n */\nvar checkIfInstanceOf = function(obj, classFunction) {\n \n};\n\n/**\n * checkIfInstanceOf(new Date(), Date); // true\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function checkIfInstanceOf(obj: any, classFunction: any): boolean {\n\n};\n\n/**\n * checkIfInstanceOf(new Date(), Date); // true\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"815\", \"totalSubmission\": \"2.9K\", \"totalAcceptedRaw\": 815, \"totalSubmissionRaw\": 2912, \"acRate\": \"28.0%\"}", "hints": [ "In Javascript, inheritance is achieved with the prototype chain.", "You can get the prototype of an object with the Object.getPrototypeOf(obj) function. Alternatively, you can code obj['__proto__'].", "You can compare an object's __proto__ with classFunction.prototype.", "Traverse the entire prototype chain until you find a match." ], "solution": null, "status": null, "sampleTestCase": "() => checkIfInstanceOf(new Date(), Date)", "metaData": "{\n \"name\": \"checkIfInstance\",\n \"params\": [\n {\n \"name\": \"func\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }