【发布时间】:2013-08-05 15:30:02
【问题描述】:
我会说,我仍然是 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,它们在 web 应用程序中交互。那么事件应该如何处理呢?您是否只需要在 全局命名空间 中实现 callbackfunctions?喜欢:
//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 标头中?也许这是一个有明显答案的愚蠢问题,但我仍然觉得好像没有任何好的最佳实践...
【问题讨论】:
-
@Stano 我再次编辑了标题,我认为这更多地反映了我的问题的真实性质..
-
@patriques 不应该是
function Monkey而不是var Monkey? -
@Oriol 是的,应该!
-
我现在找到的关于该主题的一个很好的资源是:github.com/stevekwan/best-practices/blob/master/javascript/…
标签: javascript javascript-events