{ "data": { "question": { "questionId": "2758", "questionFrontendId": "2618", "categoryTitle": "JavaScript", "boundTopicId": 2221875, "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. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.

\n\n

There are no constraints on the data types that can be passed to the function.

\n\n

 

\n

Example 1:

\n\n
\nInput: func = () => checkIfInstanceOf(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 checkIfInstanceOf(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 = () => checkIfInstanceOf(Date, Date)\nOutput: false\nExplanation: A date constructor cannot logically be an instance of itself.\n
\n\n

Example 4:

\n\n
\nInput: func = () => checkIfInstanceOf(5, Number)\nOutput: true\nExplanation: 5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".\n
\n", "translatedTitle": "检查是否是类的对象实例", "translatedContent": "

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

\n\n

可以传递给函数的数据类型没有限制。

\n\n

 

\n\n

示例 1:

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

示例 2:

\n\n
\n输入:func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }\n输出:true\n解释:\nclass Animal {};\nclass Dog extends Animal {};\ncheckIfInstance(new Dog(), Animal); // true\n\nDog 是 Animal 的子类。因此,Dog 对象同时是 Dog 和 Animal 的实例。
\n\n

示例 3:

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

示例 4:

\n\n
\n输入:func = () => checkIfInstance(5, Number)\n输出:true\n解释:5 是一个 Number。注意,\"instanceof\" 关键字将返回 false。
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 4, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {any} object\n * @param {any} 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\": \"422\", \"totalSubmission\": \"1.4K\", \"totalAcceptedRaw\": 422, \"totalSubmissionRaw\": 1359, \"acRate\": \"31.1%\"}", "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, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 4.5.4<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "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)", "__typename": "QuestionNode" } } }