1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/call-function-with-custom-context.json

59 lines
10 KiB
JSON
Raw Normal View History

2023-06-02 01:00:40 +08:00
{
"data": {
"question": {
"questionId": "2790",
"questionFrontendId": "2693",
"categoryTitle": "JavaScript",
"boundTopicId": 2275502,
"title": "Call Function with Custom Context",
"titleSlug": "call-function-with-custom-context",
"content": "<p>Enhance all functions to have the&nbsp;<code>callPolyfill</code>&nbsp;method. The method accepts an object&nbsp;<code>obj</code>&nbsp;as it&#39;s first parameter and any number of additional arguments. The&nbsp;<code>obj</code>&nbsp;becomes the&nbsp;<code>this</code>&nbsp;context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code>&nbsp;method belongs on).</p>\n\n<p>For example if you had the function:</p>\n\n<pre>\nfunction tax(price, taxRate) {\n const totalCost = price * (1 + taxRate);\n&nbsp; console.log(`The cost of ${this.item} is ${totalCost}`);\n}\n</pre>\n\n<p>Calling this function like&nbsp;<code>tax(10, 0.1)</code>&nbsp;will log&nbsp;<code>&quot;The cost of undefined is 11&quot;</code>. This is because the&nbsp;<code>this</code>&nbsp;context was not defined.</p>\n\n<p>However, calling the function like&nbsp;<code>tax.callPolyfill({item: &quot;salad&quot;}, 10, 0.1)</code>&nbsp;will log&nbsp;<code>&quot;The cost of salad is 11&quot;</code>. The&nbsp;<code>this</code>&nbsp;context was appropriately set, and the function logged an appropriate output.</p>\n\n<p>Please solve this without using&nbsp;the built-in&nbsp;<code>Function.call</code>&nbsp;method.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function add(b) {\n return this.a + b;\n}\nargs = [{&quot;a&quot;: 5}, 7]\n<strong>Output:</strong> 12\n<strong>Explanation:</strong>\nfn.callPolyfill({&quot;a&quot;: 5}, 7); // 12\ncallPolyfill sets the &quot;this&quot; context to {&quot;a&quot;: 5}. 7 is passed as an argument.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function tax(price, taxRate) { \n&nbsp;return `The cost of the ${this.item} is ${price * taxRate}`; \n}\nargs = [{&quot;item&quot;: &quot;burger&quot;}, 10, 1,1]\n<strong>Output:</strong> &quot;The cost of the burger is 11&quot;\n<strong>Explanation:</strong> callPolyfill sets the &quot;this&quot; context to {&quot;item&quot;: &quot;burger&quot;}. 10 and 1.1 are passed as additional arguments.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul style=\"list-style-type:square;\">\n\t<li><code><font face=\"monospace\">typeof args[0] == &#39;object&#39; and args[0] != null</font></code></li>\n\t<li><code>1 &lt;= args.length &lt;= 100</code></li>\n\t<li><code>2 &lt;= JSON.stringify(args[0]).length &lt;= 10<sup>5</sup></code></li>\n</ul>\n",
"translatedTitle": "使用自定义上下文调用函数",
"translatedContent": "<p>增强所有函数,使其具有&nbsp;<code>callPolyfill</code>&nbsp;方法。该方法接受一个对象&nbsp;<code>obj</code>&nbsp;作为第一个参数,以及任意数量的附加参数。<code>obj</code>&nbsp;成为函数的&nbsp;<code>this</code>&nbsp;上下文。附加参数将传递给该函数(即&nbsp;<code>callPolyfill</code>&nbsp;方法所属的函数)。</p>\n\n<p>例如,如果有以下函数:</p>\n\n<pre>\nfunction tax(price, taxRate) {\n const totalCost = price * (1 + taxRate);\n&nbsp; console.log(`The cost of ${this.item} is ${totalCost}`);\n}\n</pre>\n\n<p>调用&nbsp;<code>tax(10, 0.1)</code>&nbsp;将输出&nbsp;<code>\"The cost of undefined is 11\"</code>&nbsp;。这是因为&nbsp;<code>this</code>&nbsp;上下文未定义。</p>\n\n<p>然而,调用&nbsp;<code>tax.callPolyfill({item: \"salad\"}, 10, 0.1)</code>&nbsp;将输出&nbsp;<code>\"The cost of salad is 11\"</code>&nbsp;。<code>this</code>&nbsp;上下文被正确设置,函数输出了适当的结果。</p>\n\n<p>请在不使用内置的&nbsp;<code>Function.call</code>&nbsp;方法的情况下解决这个问题。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfn = function add(b) {\n return this.a + b;\n}\nargs = [{\"a\": 5}, 7]\n<b>输出:</b>12\n<strong>解释:</strong>\nfn.callPolyfill({\"a\": 5}, 7); // 12\n<code>callPolyfill </code>将 \"this\" 上下文设置为 <code>{\"a\": 5} </code>,并将 7 作为参数传递。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>\nfn = function tax(price, taxRate) { \n&nbsp;return `The cost of the ${this.item} is ${price * taxRate}`; \n}\nargs = [{\"item\": \"burger\"}, 10, 1,1]\n<b>输出:</b>\"The cost of the burger is 11\"\n<b>解释:</b><code>callPolyfill </code>将 \"this\" 上下文设置为 <code>{\"item\": \"burger\"} </code>,并将 10 和 1.1 作为附加参数传递。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul style=\"list-style-type:square;\">\n\t<li><code><font face=\"monospace\">typeof args[0] == 'object' and args[0] != null</font></code></li>\n\t<li><code>1 &lt;= args.length &lt;= 100</code></li>\n\t<li><code>2 &lt;= JSON.stringify(args[0]).length &lt;= 10<sup>5</sup></code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 1,
"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 {Object} context\n * @param {any[]} args\n * @return {any}\n */\nFunction.prototype.callPolyfill = function(context, ...args) {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "declare global { \n interface Function {\n callPolyfill(context: Record<any, any>, ...args: any[]): any;\n\t}\n}\n\nFunction.prototype.callPolyfill = function(context, ...args): any {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"397\", \"totalSubmission\": \"510\", \"totalAcceptedRaw\": 397, \"totalSubmissionRaw\": 510, \"acRate\": \"77.8%\"}",
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "function add(b) { return this.a + b; }\n[{\"a\":5},7]",
"metaData": "{\n \"name\": \"callPolyfill\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"context\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"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 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "function add(b) { return this.a + b; }\n[{\"a\":5},7]\nfunction tax(price, taxRate) { return `The cost of the ${this.item} is ${price * taxRate}`; }\n[{\"item\":\"burger\"},10,1.1]",
"__typename": "QuestionNode"
}
}
}