2023-04-23 22:41:08 +08:00
{
"data" : {
"question" : {
"questionId" : "2758" ,
"questionFrontendId" : "2618" ,
"categoryTitle" : "JavaScript" ,
"boundTopicId" : 2221875 ,
"title" : "Check if Object Instance of Class" ,
"titleSlug" : "check-if-object-instance-of-class" ,
2023-12-09 18:42:21 +08:00
"content" : "<p>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.</p>\n\n<p>There are no constraints on the data types that can be passed to the function. For example, the value or the class could be <code>undefined</code>.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstanceOf(new Date(), Date)\n<strong>Output:</strong> true\n<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }\n<strong>Output:</strong> true\n<strong>Explanation:</strong>\nclass Animal {};\nclass Dog extends Animal {};\ncheckIfInstanceOf(new Dog(), Animal); // true\n\nDog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstanceOf(Date, Date)\n<strong>Output:</strong> false\n<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => checkIfInstanceOf(5, Number)\n<strong>Output:</strong> true\n<strong>Explanation: </strong>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</pre>\n" ,
2023-04-23 22:41:08 +08:00
"translatedTitle" : "检查是否是类的对象实例" ,
2023-12-09 18:42:21 +08:00
"translatedContent" : "<p>请你编写一个函数,检查给定的值是否是给定类或超类的实例。</p>\n\n<p>可以传递给函数的数据类型没有限制。例如,值或类可能是 <code>undefined</code> 。</p>\n\n<p> </p>\n\n<p><strong>示例 1: </strong></p>\n\n<pre>\n<b>输入:</b>func = () => checkIfInstance(new Date(), Date)\n<b>输出:</b>true\n<strong>解释:</strong>根据定义, Date 构造函数返回的对象是 Date 的一个实例。\n</pre>\n\n<p><strong>示例 2: </strong></p>\n\n<pre>\n<b>输入:</b>func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }\n<b>输出:</b>true\n<strong>解释:</strong>\nclass Animal {};\nclass Dog extends Animal {};\ncheckIfInstanceOf(new Dog(), Animal); // true\n\nDog 是 Animal 的子类。因此, Dog 对象同时是 Dog 和 Animal 的实例。</pre>\n\n<p><strong>示例 3: </strong></p>\n\n<pre>\n<b>输入:</b>func = () => checkIfInstance(Date, Date)\n<b>输出:</b>false\n<strong>解释:</strong>日期的构造函数在逻辑上不能是其自身的实例。\n</pre>\n\n<p><strong>示例 4: </strong></p>\n\n<pre>\n<b>输入:</b>func = () => checkIfInstance(5, Number)\n<b>输出:</b>true\n<strong>解释:</strong>5 是一个 Number。注意, \"instanceof\" 关键字将返回 false。</pre>\n" ,
2023-04-23 22:41:08 +08:00
"isPaidOnly" : false ,
"difficulty" : "Medium" ,
2023-12-09 18:42:21 +08:00
"likes" : 21 ,
2023-04-23 22:41:08 +08:00
"dislikes" : 0 ,
"isLiked" : null ,
"similarQuestions" : "[]" ,
"contributors" : [ ] ,
2023-12-09 18:42:21 +08:00
"langToValidPlayground" : "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}" ,
2023-04-23 22:41:08 +08:00
"topicTags" : [ ] ,
"companyTagStats" : null ,
"codeSnippets" : [
{
"lang" : "JavaScript" ,
"langSlug" : "javascript" ,
2023-12-09 18:42:21 +08:00
"code" : "/**\n * @param {*} obj\n * @param {*} classFunction\n * @return {boolean}\n */\nvar checkIfInstanceOf = function(obj, classFunction) {\n \n};\n\n/**\n * checkIfInstanceOf(new Date(), Date); // true\n */" ,
2023-04-23 22:41:08 +08:00
"__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"
}
] ,
2023-12-09 19:57:46 +08:00
"stats" : "{\"totalAccepted\": \"5.4K\", \"totalSubmission\": \"15.7K\", \"totalAcceptedRaw\": 5356, \"totalSubmissionRaw\": 15709, \"acRate\": \"34.1%\"}" ,
2023-04-23 22:41:08 +08:00
"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 ,
2023-12-09 18:42:21 +08:00
"envInfo" : "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}" ,
2023-04-23 22:41:08 +08:00
"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"
}
}
}