zoukankan      html  css  js  c++  java
  • ES5继承

    原型继承

    <script type="text/javascript">

        function Father(){}//构造函数

        //原型属性

        Father.prototype.name = "李四";

        Father.prototype.age = 24;

        //原型方法

        Father.prototype.showName = function(){

            return this.name;

        }

        Father.prototype.showAge = function(){

            return this.age;

        }

        function Son(){}

        //原型链继承

        //Son.prototype = Father.prototype;

        //Son.prototype = new Father();

        //遍历父类的原型

        for(var i in Father.prototype){

            Son.prototype[i] = Father.prototype[i];

        }

        var son1 = new Son();

        alert(son1.showName());

        alert(son1.showAge());

    </script>

    经典继承

    <script>

        //构造函数

        function Father(name,age,money){

            //实例属性

            this.name = name;

            this.age = age;

            this.money = money;

            //实例方法

            this.showName = function(){

                return this.name;

            }

            this.showAge = function(){

                return this.age;

            }

            this.showMoney = function(){

                return this.money;

            }

        }

        function Son(name,age,money,sex){

            //经典继承、伪装继承、冒充继承(call,apply)只能继承实例

            //Father.apply(this,arguments);

            //Father.call(this,name,age,money);

            Father.apply(this,[name,age,money]);

            this.sex = sex;

            this.showSex = function(){

                return this.sex;

            }

        }

        var son1 = new Son("张三",23,20000,"男");

        alert(son1.showName());

        alert(son1.showAge());

        alert(son1.showMoney());

        alert(son1.showSex());

    </script>

    call与aplly的异同:

    第一个参数this都一样,指当前对象

    第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)

  • 相关阅读:
    2020/10/25助教一周小结(第八周)
    2020/10/18助教一周小结(第七周)
    2020/10/11助教一周小结(第六周)
    2020/10/05助教一周小结(第五周)
    2020/09/27助教一周小结(第四周)
    第三次作业总结
    第二次作业总结
    2020-11-08 助教一周小结(第十周)
    2020-11-01 助教一周小结(第九周)
    2020-10-25 助教一周小结(第八周)
  • 原文地址:https://www.cnblogs.com/twoeggg/p/8051895.html
Copyright © 2011-2022 走看看