ES6中的class类

基础语法

  • 添加属性在constructor()函数中
  • 方法写在constructor()外面
    class Person {
        constructor(name,age){//构造函数(方法),调用new,自动执行
            // console.log(`这个人叫${name},冻龄了,永远${age}`)
            this.name = name;
            this.age = age;
        }
        showName(){
            return `这sb叫${this.name}`;
        };
        showAge(){
            return `这sb年龄是${this.age}`;
        }
    }
    let p1 = new Person("DF",18);
    console.log(p1.showName());
    console.log(p1.showAge());

class类中的this矫正

  • 一般情况下,class类中的this是比较稳定的,此处如果解构p4会改变this指向
    class Person04{
        constructor() {
            this.name = "DF";
            //矫正this的指向
            this.showName = this.showName.bind(this);
        }
        showName(){
            console.log("this:",this);
            return `名字为:${this.name}`;
        }
    }
    let p4 = new Person04();

class类的静态方法

  • 所谓静态方法就是定义的类本身可调用,其实例不可调用
    class Person05{
        constructor() {
            this.name = "DF"
        }
        showName(){
            return `名字是:${this.name}`
        }
        static show(){
            return `静态方法名字是:${this.name}`
        }
    }
    let p5 = new Person05();
    console.log(p5.showName());
    console.log(Person05.show());

class类的继承

	/*父类*/
    class Person06{
        constructor(name) {
            this.name = name;
        }
        showName(){
            console.log("父类showName");
            return `父类名字是:${this.name}`
        }
    }
    /*子类继承*/
    class Student extends Person06{
        constructor(name,skill){
            super(name);//继承对应的属性
            this.skill = skill
        }
        showName(){
            super.showName();//当子类和父类方法相同时,子类方法会覆盖父类,想要执行父类方法用super
            return `子类名字是:${this.name}`
        }
        showSkill(){
            return `哥们的skill是:${this.skill}`
        }
    }
    let p6 = new Student("LOVE","健身");
    console.log(p6.showName());
    console.log(p6.showSkill());

使用class类实现拖拽功能

	/*普通拖拽*/
    class Drag{
        constructor(id){
            this.oDiv = document.querySelector(id);
            this.disX = 0;
            this.disY = 0;
            this.init();
        }
        init(){
            this.oDiv.onmousedown = function (ev) {
                this.disX = ev.clientX - this.oDiv.offsetLeft;
                this.disY = ev.clientY - this.oDiv.offsetTop;

                document.onmousemove = this.fnMove.bind(this);
                document.onmouseup = this.fnUp.bind(this);
                return false;
            }.bind(this);
        }
        fnMove(ev){
            this.oDiv.style.left = ev.clientX - this.disX+"px";
            this.oDiv.style.top = ev.clientY - this.disY+"px";
        }
        fnUp(){
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
    /*限制拖拽范围*/
    class LimitDrag extends Drag{
        fnMove(ev){
            super.fnMove(ev);
            if(this.oDiv.offsetLeft<=0){
                this.oDiv.style.left = 0
            }
            if(this.oDiv.offsetTop<=0){
                this.oDiv.style.top = 0
            }
        }
    }
    new Drag("#div1");
    new LimitDrag("#div2")
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值