JS继承

7 篇文章 0 订阅

JS继承

JS外部

// js事件arr存储参数
function MyArray() {
    this.arr = [];
    this.add = (i, e) => {
        this.arr.splice(i, 0, e);
    }
    this.info = () => {
        return this.arr.join(",");
    }
}


function DateFormat(date,format){
    this.y = date.getFullYear();
    this.mt = date.getMonth()+1;
    this.d = date.getDate();
    this.h = date.getHours();
    this.mi = date.getMinutes();
    this.s = date.getSeconds();
    this.ms = date.getMilliseconds();
    this.toString = ()=>{
        var arr = [[this.y,this.mt,this.d],[this.h,this.mi,this.s],this.ms];
        if("undefined"==typeof(format)){
            arr = [arr[0].join("-"),arr[1].join(":"),arr[2]];
            return arr.join(" ");
        }else{
            //yyyy-MM-dd
            //yyyy-MM-dd HH:mm:ss
            //yyyy-MM-dd HH:mm:ss SSS
        }
    }
}

html

<body>
    <script src="/common.js"></script>
    <script>
        function Person(name,age,gender,phone){
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.phone = phone;
            this.info2=()=>{
                return [this.name,this.age,this.gender,this.phone].join(",");
            }
        }
        //普通方法
        // function Student1(name,age,gender,phone,stuId,className){
        //     this.person = new Person(name,age,gender,phone);
        //     this.stuId = stuId;
        //     this.className = className;
        //     this.info1 = ()=>[this.person.info(),this.studId,this.className].join(",");
        // }
        // 使用call方法
        function Student2(name,age,gender,phone,stuId,className){
            Person.call(this,name,age,gender,phone);    //这个时候的Person中的this已经被Student2所代替
            this.stuId = stuId;
            this.className = className;
            this.info=()=>{
                return [this.info2(),this.stuId,this.className].join(",");
            }
        }
        // 使用apply方法
        function Student3(arr,stuId,className){
            Person.apply(this,arr);  
               //arr可能为父类参数列表
            this.stuId = stuId;
            this.className = className;
            this.info = () =>{
                return[this.info2(),this.stuId,this.className].join(",");
            }
        }
        // call()和apply()两者的用法是一样的等效,唯一的区别就是call后面跟的是
        // 有个一个一个单独的数据,而apply需要把数据放在数组里面。
        // var p = new Person('貂蝉',18,'女','1234');
        // console.log(p.info());
        
        var s = new Student2('貂蝉',18,'女','1234','22','kh69');
        var v = new Student2('西施',18,'女','1235',001,'kh69');
        var r = new Student3(['王昭君',18,'女','1236'],006,'kh69');
        // 非常注意,以至于警告:实参里面记得写[]!!!如['王昭君',18,'女','1236'] 
        console.log(s.info());
        console.log(v.info())
        console.log(r.info())
        console.log(s.name)
        // 添加元素到数组
        var me = new MyArray();
        me.add(0,"bb");
        me.add(0,"xx");
        me.add(1,"ss");
        console.log(me.info());
        var arr = ["aa","dd"];
        arr.add(1,"cc");
        console.log(arr);
        // 日期表示
        var df = new DateFormat(new Date());
        console.log(df.toString());
        Array.prototype.add = function(i,e){    //若用(i,e)=> this表示window
            this.splice(i,0,e);     //若用function(i,e) this表示Array
        }
        Array.prototype.remove=function(i,count){
            this.splice(i,count);
        }
    </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值