列表

详情


JS18. 继承

描述

请补全JavaScript代码,实现以下功能:
1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
2. 将"Chinese"构造函数继承于"Human"构造函数
3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1747ms, 内存消耗: 77816KB, 提交时间: 2021-12-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function Human(name) {\n                this.name = name\n                this.kingdom = 'animal'\n                this.color = ['yellow', 'white', 'brown', 'black']\n            }\n            \n            function Chinese(name,age) {\n                Human.call(this,name)\n                this.age = age\n                this.color = 'yellow'\n            }\n\n            // 补全代码\n            Human.prototype.getName = function() {\n                return this.name;\n            };\n            Chinese.prototype = new Human();\n            Chinese.prototype.constructor = Chinese;\n            Chinese.prototype.getAge = function() {\n                return this.age;\n            }\n            \n        </script>\n    </body>\n</html>","libs":["jquery","underscore"]}

HTML/CSS/JavaScript 解法, 执行用时: 1750ms, 内存消耗: 77900KB, 提交时间: 2021-12-16

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function Human(name) {\n                this.name = name\n                this.kingdom = 'animal'\n                this.color = ['yellow', 'white', 'brown', 'black']\n            }\n            \n            function Chinese(name,age) {\n                Human.call(this,name)\n                this.age = age\n                this.color = 'yellow'\n            }\n\n            // 补全代码\n            Human.prototype.getName = function(){\n                return this.name\n            }\n            Chinese.prototype = new Human()\n            Chinese.prototype.constructor = Chinese\n            Chinese.prototype.getAge=function(){\n                return this.age\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1752ms, 内存消耗: 77900KB, 提交时间: 2021-12-05

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function Human(name) {\n                this.name = name\n                this.kingdom = 'animal'\n                this.color = ['yellow', 'white', 'brown', 'black']\n            }\n            \n            function Chinese(name,age) {\n                Human.call(this,name)\n                this.age = age\n                this.color = 'yellow'\n            }\n\n            // 补全代码\n            Chinese.prototype = new Human();\n            Chinese.prototype.constructor = Chinese;\n            Human.prototype.getName = function() {\n                return this.name;\n            }\n            Chinese.prototype.getAge = function() {\n                return this.age;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1755ms, 内存消耗: 77900KB, 提交时间: 2021-12-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function Human(name) {\n                this.name = name\n                this.kingdom = 'animal'\n                this.color = ['yellow', 'white', 'brown', 'black']\n            }\n            Human.prototype.getName = function(){\n                return this.name\n            }\n            \n            function Chinese(name,age) {\n                Human.call(this,name)\n                this.age = age\n                this.color = 'yellow'\n            }\n            Chinese.prototype = new Human()\n            Chinese.prototype.constructor = Chinese\n            Chinese.prototype.getAge = function(){\n                return this.age\n            }\n            // 补全代码\n            \n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1758ms, 内存消耗: 77776KB, 提交时间: 2022-01-11

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            function Human(name) {\n                this.name = name\n                this.kingdom = 'animal'\n                this.color = ['yellow', 'white', 'brown', 'black']\n            }\n            \n            function Chinese(name,age) {\n                Human.call(this,name)\n                this.age = age\n                this.color = 'yellow'\n            }\n\n            // 补全代码\n            Human.prototype.getName = function () {\n  return this.name;\n};\nChinese.prototype.__proto__ = Human.prototype;\nHuman.prototype.getAge = function () {\n  return this.age;\n};\n        </script>\n    </body>\n</html>","libs":[]}

上一题