| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1759 人关注过本帖
标题:prototype继承,,显示不出来呀
只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
 问题点数:0 回复次数:2 
prototype继承,,显示不出来呀
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <script>
        function Car()        //基类
        {
            this.color="white";
            this.size="middle";
            this.set=function set(color,size){this.color=color;this.size=size;}
            this.run=function(){return "I can run";}
            this.voice=function() {return "I can voice";}
        }
        function Bus()     //子类
        {
            Bus.prototype=new Car();
            Bus.prototype.constructor=Bus;
             this.kkk=function kkk(){return "KKK";}
        }
        function Truck()
        {
            Truck.prototype=new Truck();
            Truck.prototype.constructor=Truck;
            this.kkk=function kkk(){return "kkk";}
        }
        var bus=new Bus();
        bus.set("white","middle");
        alert(bus.color);
        alert(bus.run());
        var truck=new Truck();
        truck.set("grey","big");
        alert(truck.size);
        alert(truck.voice());
        </script>
    </body>
    </html>
搜索更多相关主题的帖子: prototype function this color size 
2018-06-24 17:08
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
程序代码:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <script>
        function Car()        //基类
        {
            this.color="white";
            this.size="middle";
            this.set=function set(color,size){this.color=color;this.size=size;}
            this.run=function(){return "I can run";}
            this.voice=function() {return "I can voice";}
        }
        function Bus()     //子类
        {
            //Bus.prototype.constructor=Bus;
            //this.kkk=function kkk(){return "KKK";}
        }
        Bus.prototype=new Car();
        function Truck()
        {
            //Truck.prototype.constructor=Truck;
            //this.kkk=function kkk(){return "kkk";}
        }
        Truck.prototype=new Car();
        var bus=new Bus();
        bus.set("white","middle");
        alert(bus.color);
        alert(bus.run());
        var truck=new Truck();
        truck.set("grey","big");
        alert(truck.size);
        alert(truck.voice());
        </script>
    </body>
    </html>

剑栈风樯各苦辛,别时冰雪到时春
2018-07-11 21:17
leeqihero
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:122
专家分:135
注 册:2016-3-24
收藏
得分:0 
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script>
Car={
    color:'white',
    size:'middle',
    run:function(){
        return 'I can run';
    },
    voice:function(){
        return 'I can voice';
    }
};
bus={__proto__:Car};
truck={color:'grey',size:'big',__proto__:Car};
console.log(bus.color);
console.log(bus.run());
console.log(truck.color);
console.log(truck.run());
</script>
</body>
</html>
2018-07-24 17:46
快速回复:prototype继承,,显示不出来呀
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015804 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved