首页 技术 正文
技术 2022年11月15日
0 收藏 952 点赞 2,185 浏览 1399 个字

贴上一段同事写的代码,值的纪念

<script type="text/javascript">    //创建箱子类
function Box(option) {
var self = this;
var _option = {
height: 12,
width: 12
};
//合并参数对象 : 记得引用Jquery.js
$.extend(_option, option); this.height = _option.height;
this.width = _option.height;
//绑定事件列表
var _events = {}; //创建一个方法,并带有回调函数
this.push = function(option, callback) {
if (option.height > this.height) {
console.error("over height", this);
return;
}
if (option.width > this.width) {
console.error("over width", this);
return;
} //判断是否有回调函数
if (callback instanceof Function)
//调用回调函数,并给它传值(参数:_option)
callback.call(this, _option);
} //绑定事件
this.on = function(name, event) {
if (name == null || !name)
return null; if (!(event instanceof Function))
if (self[name] instanceof Function) {
return self[name]();
} if (event instanceof Function)
_events[name] = self[name] = function() {
event.apply(this);
return this;
}
} //解除绑定事件
this.unbind = function(name) {
delete self[name];
delete _events[name]; //链式表达式
return this;
}
} //创建box1对象
var box1 = new Box(); //创建box2对象
var box2 = new Box(); //调用方法
box1.push({
height: 12,
width: 12
//回调函数
}, function(option) { console.log("push回调函数已调用..", option);
}); //动态给对象绑定事件
box1.on("click", function() {
this.width += 10;
console.log("width递增10,click事件被调用..", this.width);
});
box1.on("heihei", function() {
console.log("heihei事件被调用..", this.width);
}); //事件普通调用
box1.click();
//链式表达式调用方法
box1.on("click").click().heihei().on("heihei"); //打印box1的宽度
console.log(box1.width);
//打印box2的宽度
console.log(box2.width); //解除绑定事件
box1.unbind("heihei");
//测试解除
box1.heihei();</script>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905