JS面向对象、prototype、call()、apply()和实例

文章排版有问题,首先是转载,地址:http://jaychaoqun.javaeye.com/blog/392110

接下来是理解之后的实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简易拖放效果</title>
</head>
<body>
<script>
//参考:http://www.javaeye.com/topic/57760
//http://jaychaoqun.javaeye.com/blog/392110
 
//简化id选择器
var isIE = (document.all) ? true : false;
var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};
 
//初始化一个 Class 对象, 它有一个成员,是一个方法, 这个方法返因另一个方法(方法是对象,所以可以作为参数或者返回值)
var Class = {
    create: function() {
        return function() { //使用new操作符时,就会在新产生的对象上调用这个方法
            this.initialize.apply(this, arguments); //这里就是调用 this 对象上的 initialize方法, 并传递 arguments,这里的this是new之后构造出来的对象
        }
    }
}
 
//继承,这里没有用到
var Extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
}
 
//参数传递
//object参数,fun方法
var Bind = function(object, fun) {
    return function() {
        return fun.apply(object, arguments);
    }
}
 
//事件传递
//object参数,fun方法
var BindAsEventListener = function(object, fun) {
    return function(event) {
        return fun.call(object, (event || window.event));
    }
}
 
//事件绑定
//oTarget绑定目标,sEventType事件类型,fnHandler事件方法
function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) { //FF
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {   //IE
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};
 
//移除事件绑定
//oTarget绑定目标,sEventType事件类型,fnHandler事件方法
function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {  //FF
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {   //IE
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = null;
    }
};
 
//SimpleDrag是一个方法,方法体,这里相当于一个类
var SimpleDrag = Class.create();
SimpleDrag.prototype = {
  //对象初始化,实现Class中的this.initialize
  initialize: function(drag) {
    this.Drag = $(drag);    //绑定目标
    this._x = this._y = 0;  //初始值
    this._fnMove = BindAsEventListener(this, this.Move);
    this._fnStop = Bind(this, this.Stop);
    this.Drag.style.position = "absolute";
    addEventHandler(this.Drag, "mousedown", BindAsEventListener(this, this.Start));//监听鼠标按下事件
  },
  //准备拖动
  Start: function(oEvent) {
    this._x = oEvent.clientX - this.Drag.offsetLeft;
    this._y = oEvent.clientY - this.Drag.offsetTop;
    addEventHandler(document, "mousemove", this._fnMove);   //对象开始拖动后,监听鼠标移动事件
    addEventHandler(document, "mouseup", this._fnStop);     //同时监听鼠标放开事件
  },
  //拖动
  Move: function(oEvent) {
    this.Drag.style.left = oEvent.clientX - this._x + "px";
    this.Drag.style.top = oEvent.clientY - this._y + "px";
  },
  //停止拖动
  Stop: function() {
    removeEventHandler(document, "mousemove", this._fnMove);
    removeEventHandler(document, "mouseup", this._fnStop);
  }
};
</script>
<div id="idDrag" style="border:5px solid #0000FF; background:#C4E3FD; width:50px; height:50px;"></div>
<script>
new SimpleDrag("idDrag");
</script>
</body>
</html>
posted @   黑曜石  阅读(2171)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示