JS类使用方法 待补充

    class Human {
        constructor(name, age) {
            //name age 是传参
            this.name = name;
            this.age = age
        }
    }

    let ren = new Human("小明", 100);
    // 使用Human类,实例化类
    console.log(ren.name);
    console.log(ren.age);

    // 第二种写法推荐====================================
    class Human1 {
        constructor() {
            //name age 是传参
            let a = {...{name: "xiaoliu", age: 10}, ...arguments[0]};
            this.name = a.name;
            this.age = a.age;
            //创建方法
            this.eat= function (food="默认食物") {
                 // 在方法中使用属性
                console.log(this.name+"在吃",food);

            }
        }
    }

    let ren1 = new Human1({name: "小李", age: 30});
    // 使用Human类,实例化类
    console.log(ren1.name);
    console.log(ren1.age);
    ren1.eat("小面包");
    console.log("++++++++++++++++++++++");



    // ===============================================
    //         关于...的用法
    //合并数组
    console.log("合并数组")
    let obj1 = [10, 50, true, "Fly"];
    console.log(obj1)
    let obj2 = [20, 60, false, "Land"];
    let obj3 = [...obj1, ...obj2];
    let objls=[obj1,obj2]
    console.log(obj3);
    console.log(objls)
    // ...扩展运算符 注意后来这居上  最后的结果是obj6=obj5
    let obj4={name:"小明",age:10};
    let obj5={name:"小娟",age:20};
    let obj6={...obj4,...obj5};
    console.log(obj6)
    // ==============================================

 

JS类使用方法 待补充

上一篇:js小demo-迫使页面总是单独显示,不能被嵌入到iframe中


下一篇:深入学css盒子模型