Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
继承codecademy javascript教程_Javascript - Fatal编程技术网

继承codecademy javascript教程

继承codecademy javascript教程,javascript,Javascript,比如说,我们正在和很多企鹅打交道。创建一个Penguin类当然很好,这样也许以后我们可以为它提供一些企鹅特有的方法,而不会将它与Animal类混淆 说明书 创建一个新的企鹅类构造函数。企鹅是一种动物,因此它也应该具有name和numLegs属性以及sayName方法,该方法可以打印与动物的sayName方法相同的内容 我们还没有完成对动物的研究,所以我们仍然包括了动物构造器及其sayName方法。最后两行测试企鹅代码 我的代码哪里出错了 function An

比如说,我们正在和很多企鹅打交道。创建一个Penguin类当然很好,这样也许以后我们可以为它提供一些企鹅特有的方法,而不会将它与Animal类混淆

说明书 创建一个新的企鹅类构造函数。企鹅是一种动物,因此它也应该具有name和numLegs属性以及sayName方法,该方法可以打印与动物的sayName方法相同的内容

我们还没有完成对动物的研究,所以我们仍然包括了动物构造器及其sayName方法。最后两行测试企鹅代码

我的代码哪里出错了

                  function Animal(name, numLegs) {
                    this.name = name;
                  this.numLegs = numLegs;
                      }
                     Animal.prototype.sayName = function() {
                     console.log("Hi my name is "+this.name);
                };

            // create a Penguin constructor here

                  var penguin = new Penguin(name,numLegs);
                 function sayName(){

                 console.log("Hi, this is" + this.name);
                  };

                  // create a sayName method for Penguins here


                   // our test code
                   var theCaptain = new Penguin("Captain Cook", 2);
                  theCaptain.sayName();
                  var penguin = new Penguin("captain1",54)

可能不是练习要求的,但我会这样做:

function Penguin () {
    Animal.apply(this, arguments);   
}
Penguin.prototype = Object.create(Animal.prototype);
Penguin.prototype.constructor = Penguin;

上面的代码使企鹅实例从Animal继承(使用
sayName
方法)。此外,Animal构造函数用于设置企鹅实例的自身属性(腿的名称和数量)。

这是练习的正确代码:

function Animal(name, numLegs) {
                    this.name = name;
                  this.numLegs = numLegs;
                      }
                     Animal.prototype.sayName = function() {
                     console.log("Hi my name is "+this.name);
                };

            // create a Penguin constructor here

                function Penguin(name, numLegs) {
                    this.name = name;
                  this.numLegs = numLegs;
                      }
                     Penguin.prototype.sayName = function() {
                     console.log("Hi my name is "+this.name);
                };
// provided code to test above constructor and method
var penguin = new Penguin("Captain Cook", 2);
penguin.sayName();

什么是它不应该做的,或者它不应该做的?嗨@RtrRtr!堆栈溢出不是为代码指导而设计的-问题旨在详细描述您遇到的问题,以便遇到相同问题的其他人可以从问题及其后续答案中受益。你能描述一下你期望你的代码做什么,以及它实际做什么吗?(另请参阅)可能的副本我们在这里也不是作为我的导师免费的个人代码。好吧,我不是瞎子,我读了你的第一条消息。