I explain everything in the comments, I'm trying to refactor my code, The code now is much better than before, But I have now global variables (I think).
var app = {};
// Let's say we have a commenting appliation(wow!)
app.comment = {
init: function() {
// Bind the DOM event on startup.
this.bindEvents();
},
bindEvents: function() {
$(document).on('keypress', '.comment-input', this.comment);
},
comment: function() {
// handle the commenting stuff...
$.ajax();
// But now I can't use the var keyword, Because of scoping issue.
// If I do something like this:
var $foo = $('someElement');
// It won't be available to renderCreatedComment method.
// So I have to declare it globally
$foo = $('someElement');
this.renderCreatedComment();
// Most developer don't recommend the use of global variables.
},
renderCreatedComment: function() {
// Render the posted comment and update the comment count
}
}
// Somewhere in my code...
app.comment.init();
Most developers don't recommend the use of global variables.
So is this a bad pattern?
Aucun commentaire:
Enregistrer un commentaire