首页 > 编程语言 > 详细

【笔记】JavaScript编码规范- 事件&模块

时间:2015-05-20 11:26:15      阅读:203      评论:0      收藏:0      [点我收藏+]

当在事件对象上附加数据时(无论是DOM事件还是如Backbone一样拥有的私有事件),应传递散列对象而不是原始值,这可以让随后的贡献者给事件对象添加更多的数据,而不必去查找或者更新每一个事件处理程序。举个粟子,不要用下面的方式:

// bad
$(this).trigger('listingUpdated', listing.id);
...
$(this).on('listingUpdated', function(e, listingId) {
// do something with listingId
});

应该按如下方式:
// good
$(this).trigger('listingUpdated', { listingId : listing.id });
...
$(this).on('listingUpdated', function(e, data) {
// do something with data.listingId
});

模块应该以 ! 开始,这能确保当脚本连接时,如果畸形模块忘记导入,包括最后一个分号,不会产生错误。Explanation

文件应该以驼峰式命名,放在同名的文件夹中,和单出口的名称相匹配

定义一个noConflict()方法来设置导出模块之前的版本,并返回当前版本。

在模块的顶部申明’use strict‘;

// fancyInput/fancyInput.js


!function(global) {
'use strict';


var previousFancyInput = global.FancyInput;


function FancyInput(options) {
this.options = options || {};
}


FancyInput.noConflict = function noConflict() {
global.FancyInput = previousFancyInput;
return FancyInput;
};


global.FancyInput = FancyInput;
}(this);

Genesis 1:18 And rule over the day and over the night,and to divide the light from the darkness:and God saw that it was good.

【笔记】JavaScript编码规范- 事件&模块

原文:http://blog.csdn.net/princeterence/article/details/45866497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!