请补全JavaScript代码,实现以下功能:
1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
2. 将"Chinese"构造函数继承于"Human"构造函数
3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性
function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function () { return this.name } function Chinese(name, age) { Human.call(this, name) this.age = age this.color = 'yellow' } // 组合继承 Chinese.prototype = new Human() Chinese.prototype.constructor = Chinese Chinese.prototype.getAge = function () { return this.age } let ans = function () { let me = new Chinese('me', 1); return me.getName() === 'me' && me.getAge() === 1 } console.log(ans()) let c = new Chinese('12', 21) console.log(c.getAge())
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function () { return this.name } function Chinese(name, age) { Human.call(this, name) this.age = age this.color = 'yellow' } // 寄生组合继承 (function () { var Super = function () { }; Super.prototype = Human.prototype; Chinese.prototype = new Super() })() Chinese.prototype.getAge = function () { return this.age } let ans = function () { let me = new Chinese('me', 1); return me.getName() === 'me' && me.getAge() === 1 } console.log(ans()) let c = new Chinese('12', 21) console.log(c.getAge()) </script> </body> </html>
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Human.prototype.getName = function() { return this.name; } Chinese.prototype = new Human(); Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge = function() { return this.age; } </script>
// 1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性 Human.prototype.getName = function() { return this.name } // 2. 将"Chinese"构造函数继承于"Human"构造函数 Chinese.prototype = new Human() Chinese.prototype.constructor = Chinese // 3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性 Chinese.prototype.getAge = function() { return this.age }
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Chinese.prototype = Object.create(Human.prototype) Human.prototype.getName = function (){ console.log("this,name",this.name); return this.name } Chinese.prototype.getAge = function (){ console.log("this,age",this.age); return this.age } </script>
function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { // Human.call(this, name) this.age = age this.color = 'yellow' } // 补全代码 Human.prototype.getName=function(){ return this.name } // 以构造函数Human的原型对象为原型,创建新对象,将新对象作为构造函数Chinese的原型对象 Chinese.prototype = Object.create(Human.prototype); // 构造函数Chinese的原型对象prototype的constructor属性,指回构造函数Chinese Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge=function(){ return this.age }
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function(){ return this.name; } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } //最关键的一句 Chinese.prototype = Object.create(Human.prototype); Chinese.prototype.getAge = function(){ return this.age; } // 补全代码 </script>
<!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Human.prototype.getName = function(){ return this.name } console.log(Chinese.prototype) Chinese.prototype = Human.prototype Chinese.prototype.constructor = Chinese Chinese.prototype.getAge = function (){ return this.age } let me = new Chinese("yaohf",21) console.log( me.getAge()) console.log(me.getName()) </script> </body> </html>
function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { //继承属性 // 第二次调用Human Human.call(this,name) this.age = age this.color = 'yellow' } Human.prototype.getName = function() { return this.name; } // 继承方法-构造原型链 // 第一次调用Human Chinese.prototype = new Human(); // 重写SubType.prototype的constructor属性,指向自己的构造函数SubType Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge = function() { return this.age; }寄生组合式继承
function inheritPrototype(subType, superType){ var prototype = Object.create(superType.prototype); // 创建对象,创建父类原型的一个副本 prototype.constructor = subType; // 增强对象,弥补因重写原型而失去的默认的constructor 属性 subType.prototype = prototype; // 指定对象,将新创建的对象赋值给子类的原型 } function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } Human.prototype.getName = function() { return this.name; } inheritPrototype(Chinese, Human); Chinese.prototype.getAge = function() { return this.age; }
function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function () { return this.name } function Chinese(name, age) { Human.call(this, name) this.age = age this.color = 'yellow' } // 补全代码 Chinese.prototype = new Human(); Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge=function(){ return this.age }
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Human.prototype.getName = function() { return this.name; } Chinese.prototype = new Human(); Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge = function() { return this.age; } </script> </body></body></html>
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function(){return this.name;} function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Chinese.prototype = Human.prototype; Chinese.prototype.getAge = function(){return this.age;} </script>
class Human { constructor(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } // 补全代码 getName() { return this.name } } // 补全代码 class Chinese extends Human { constructor(name, age) { super(name) this.name = name this.age = age } getAge() { return this.age } }跟第13题有什么不一样吗?
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } Human.prototype.getName = function(){ return this.name; } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } Chinese.prototype.getAge = function(){ return this.age; } </script>
<script type="text/javascript"> function Human(name) { this.name = name this.kingdom = 'animal' this.color = ['yellow', 'white', 'brown', 'black'] } function Chinese(name,age) { Human.call(this,name) this.age = age this.color = 'yellow' } // 补全代码 Human.prototype.getName = function(){ return this.name; } Chinese.prototype = new Human(); Chinese.prototype.constructor = Chinese; Chinese.prototype.getAge=function(){ return this.age; } </script>