JS继承

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>

你可能感兴趣的:(前端,js,JavaScript继承)