I'm working on a project and I'm really trying to write object-oriented JavaScript code. I have just started reading Douglas Crockford's JavaScript: The Good Parts and I'm quickly beginning to realize that writing Java-esque OOP in JavaScript will be a difficult task.
Thus far, I've written something like the following...
// index.html
$(document).ready(function() {
$().SetUpElements();
});
// this is in a different js file
$.fn.SetUpElements = function() {
// do stuff here
$().UpdateElement();
};
// this is in yet another different js file
$.fn.UpdateElement = function() {
// do stuff here
var element = new Element(id, name); // continue doing work
};
function Element(id, name) {
var id = id;
var name = name;
// other stuff
};
... the idea being that I want objects/functions to be refactored and decoupled as much as possible; I want to reuse as much code as I can. I've spread a lot of my code across different .js files with the intention of grouping specific relevant code together, much like if you would write different classes in Java.
As I've been learning more about jQuery, I realized that the notation $.fn.foo = function() { ... };
is actually adding this foo
function to the prototype of all jQuery objects. Is this something I should be doing? Am I misusing jQuery somehow?
I would appreciate suggestions on how to improve my approach to OOP in JavaScript and I would love to see references to sources/tutorials/articles/etc... that discuss this topic. Please feel free to provide feedback even if an answer has been selected. I am looking for your advice... this is why I posted :)
** Note: I'm not developing a jQuery plugin. I'm developing a web app and heavily making use of jQuery.