全局命名空间中事件的最佳实践
Best practices for events in global namespace?
我还是JavaScript的新手,所以我认为我现在处理项目的方式在某种程度上影响了我思考如何在js中完成事情的方式。我已经明白,在js中编程时,最好使用模块。这让我开始思考js中的事件。例如,假设我有这个对象Monkey
:
function Monkey(color, position){
this.color = color;
this.position = position;
this.jumpAround = function() {
/* jumps around and screams */
};
}
假设我已经构建了一个完整的应用程序,包括monkeys
、giraffes
和shih tzus
,它们在网络应用程序中进行交互。那么应该如何处理这些事件呢?是否只需在全局命名空间中实现回调函数?类似:
//objects
var monkey = new Monkey(brown, pos);
var giraffe = new Giraffe(yellow, pos);
var shih_tzu = new Shih_tzu(white, pos);
//event handlers
this_and_that.onclick = function() { /* this and that happens */ }
...
然后将此文件包含在html标头中?也许这是一个愚蠢的问题,有一个显而易见的答案,但对我来说,似乎没有任何好的最佳实践。
您可以将所有代码放入匿名自调用函数中,这也将创建闭包:
(function(){
// create all your objects and define all events handlers here
})();
这样您的代码就不会污染全局命名空间,并且从外部无法访问。所有的事件处理程序都将在闭包中执行。
(注意:你也可以在jQuery库中找到它。在脚本文件的最后是暴露在外部世界的jQuery对象:window.jQuery = window.$ = jQuery;
)
我不完全确定我是否理解这个问题,但如果你的意思是处理由重复elem.onclick = function() {}
引起的javascript中事件的覆盖,我使用这个函数:
function addEvent(obj,event,func)
{
if(typeof func !== 'function')
{
return false;
}
if(typeof obj.addEventListener == 'function' || typeof obj.addEventListener == 'object')
{
return obj.addEventListener(event.replace(/^on/,''), func, false);
}
else if(typeof obj.attachEvent == 'function' || typeof obj.attachEvent == 'object')
{
return obj.attachEvent(event,func);
}
}
addEvent(this_and_that,'onclick',function(e) {
//do stuff
});
这里有一个关于对象继承的充实示例。http://jsfiddle.net/H4jqF/
CSS-只是一个基本的动物对象,具有0.5秒的平滑过渡,用于属性更改时(例如使我们的动物JUMP)。
.animal {
position: absolute;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
}
JavaScript
// simple HTML animal - parent class
function Animal(color, position) {
this.color = color;
this.position = position;
this.elm = document.createElement("IMG");
this.elm.className = "animal";
this.elm.style.backgroundColor = this.color;
this.update = function() {
// update the the HTML element
this.elm.style.left = this.position.x + "px";
this.elm.style.top = this.position.y + "px";
};
this.jump = function(height) {
// cheesy jump animation
// we'll use a CSS transition to smooth it out
this.position.y -= height;
this.update();
// hard code it to come back down in 500ms
// .bind(this) is used to scope the function to this instance
window.setTimeout(function() {
this.position.y += height;
this.update();
}.bind(this), 500);
};
this.update();
}
// our subclass Dog
function Dog(color, position) {
// inherit all of the parent objects properties and methods
Animal.apply(this, arguments);
// finish setup of the element
this.elm.src = "dog.png";
document.body.appendChild(this.elm);
// add our onclick event that will fire jump
this.elm.onclick = function() {
this.jump(100);
}.bind(this);
}
// spawn some dogs
new Dog("red", {x: 50, y: 200});
new Dog("blue", {x: 200, y: 200});
相关文章:
- 什么'是在HTML5画布中创建关键事件的最佳方式
- 在Nodejs中堆叠异步回调事件的最佳方式
- addEvent()传奇事件之后的当前最佳实践是什么
- 主干.js事件处理程序命名的最佳做法
- 在 Web 应用中处理事件跟踪的最佳(高性能)方法
- 将 react-redux 与基于事件的第三方库一起使用的最佳方法是什么?
- 用于查找基于时间的事件的最佳Javascript算法
- 通知全局事件组件的最佳方式
- 处理多次单击事件的最佳方式
- 在大型系统中附加事件处理程序的最佳做法
- 为输入字段(而不是表单的一部分)设置默认按钮(或在 javascript 中触发其事件)的最佳方法
- 当文档中的单个元素准备就绪时触发事件的最佳方法
- Javascript:注册事件处理程序的最佳位置
- 不再需要视图后取消委派事件的最佳方法
- React,在组件事件上呈现消息的最佳方法
- 这真的是从事件回调中获取 Rx.Observable 的最佳方式吗?
- 使用jsduck记录事件处理程序的最佳方式是什么
- 什么'是在AngularJS中处理应用程序状态和事件广播的最佳实践
- 从中停用单击事件的最佳方式是什么
- 跟踪javascript中文本框控件上触发的退格事件位置的最佳方法