码迷,mamicode.com
首页 > 其他好文 > 详细

16-1 ECMA5与ECMA6的函数定义

时间:2018-07-06 22:22:58      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:str   fse   定义   sem   pre   get   mouse   tor   函数   

ECMA5:

function Drag(id){
    this.ele = document.getElementById(id);
    var that = this;
    this.ele.onmousedown = function(evt){
        that.fnDown(evt);
    }
    this.fnDown = function(evt){
        var e = evt || window.event;
        this.disX = e.offsetX;
        this.disY = e.offsetY;
        var that = this;
        document.onmousemove = function(evt){
            that.fnMove(evt);
        }
        document.onmouseup = this.fnUp;
    }
    this.fnMove = function(evt){
        var e = evt || window.event;
        this.ele.style.left = e.pageX - this.disX + ‘px‘;
        this.ele.style.top = e.pageY - this.disY + ‘px‘;
    }
    this.fnUp = function(){
        document.onmousemove = null;
    }
}



ECMA6:

class Drag{
    constructor(id){
        //属性
        this.ele = document.getElementById(id);
        var that = this;
        this.ele.onmousedown = function(evt){
            that.fnDown(evt);
        }
    }
    fnDown(evt){
        var e = evt || window.event;
        this.disX = e.offsetX;
        this.disY = e.offsetY;
        var that = this;
        document.onmousemove = function(evt){
            that.fnMove(evt);
        }
        document.onmouseup = this.fnUp;
    }
    fnMove(evt){
        var e = evt || window.event;
        this.ele.style.left = e.pageX - this.disX + ‘px‘;
        this.ele.style.top = e.pageY - this.disY + ‘px‘;
    }
    fnUp(){
        document.onmousemove = null;
    }
}

16-1 ECMA5与ECMA6的函数定义

标签:str   fse   定义   sem   pre   get   mouse   tor   函数   

原文地址:https://www.cnblogs.com/zhongchao666/p/9275566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!