继承

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
    //继承的作用:精简代码
        function Dad (name,house,car,money) {
            this.name = name;
            this.house = house;
            this.car = car;
            this.money = money;
            this.sayName = function () {
                alert(this.name);
            }
            this.hobby = function () {
                alert("轰趴,飙车");
            }
        }
        Dad.prototype.fight = function () {
            alert("打架");
        }
        function Son (name,house,car,money) {
            this.job = "吃喝玩乐";
            this.name = name;
            //继承使用call和apply 
//          Dad.call(this,name,house,car,money);
            Dad.apply(this,arguments)
        }
//      Son.prototype = Dad.prototype;  公用地址,任意改变都会改变

        Son.prototype = new Dad();
        Son.prototype.constructor = Son;
        
        
        
        var son1 = new Son("大儿子","复式别墅","宝马",1000000000000000000000);
        alert(son1.car);
        console.log(son1);
        son1.fight();
    </script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容