我在考虑什么最适合我的 JavaScript 代码。
我有一个包含一些表单的 HTML 页面。所以我用 JavaScript 给它带来生命,我们开始吧。
我正在考虑利用我的 JavaScript 代码的最佳方法。
首先创建一个包含我需要的所有属性和方法的 JSON 对象。
例如
var myObject={
menuBtn: null,
init: function(){
$(document).ready(function(){
/* the code will be executed after the load */
});
},
listenToMenuButton: function(){
/* add a listener to the button */
}
};
myObject.init(); // to start working
或者代替那个
(function($){
var me = this;
me.menuBtn = null;
me.listenToMenuButton = function(){
/* add a listener to the button */
};
me.init=function(){
/* code to init all elements */
};
})(jQuery);
谁能告诉我什么是最好的以及(两种方法的)优点是什么?
例如,我知道第一个实现允许我根据需要多次调用方法。但它的缺点是用户可以对其进行一些破解并调用它(例如来自 firebug)并应用一个虚假的行为/
我什么时候应该开发一个扩展(jQuery)?
我希望你们能帮助我设计好我的代码。