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

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

接下来是理解之后的实例

<!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 @ 2010-10-14 11:18  黑曜石  阅读(2169)  评论(0编辑  收藏  举报