老师请教一个问题,

来源:6-4 编程练习

Young_W

2020-06-06 11:10:54

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        //  补充代码 
        function Animal(name, count){
            this.name = name;
            this.count = count;
        }
        
        function Cat(){}
        
        Cat.prototype = new Animal("小猫",20);
        
        Cat.prototype.info = function(name,count){
            alert("动物名称是:"+this.name+" 数量是:" +this.count);
        }
        
        var c = new Cat();
        c.info();
    </script>
</body>
</html>
代码中是通过 new Animal("小猫",20)的方法传值的,那么有没有办法实现直接给Cat对象传值进行实例化对象呢?
即 var c = new Cat("小猫",20) 通过这条语句实例化c,弹出相对应的信息。


写回答

1回答

好帮手慕慕子

2020-06-06

同学你好,目前学习的原型继承无法实现同学说的这种场景。

同学不用着急,后面老师会讲解其他方式继承方式,老师这里先给同学举个例子,不用纠结具体是什么意思,下一节课老师就会讲了,同学先了解下有这种方式即可,

http://img.mukewang.com/climg/5edb300f0916068007720392.jpg

如果我的回答帮助到了你,欢迎采纳 ,祝学习愉快~

0

0 学习 · 14456 问题

查看课程