I’ve come to admire the simplicity and style of jquery. There are some cons to it’s approach of using context (as can be seen as this in the for instance “each”):
jQuery("#table tr").each(function(){
jQuery(this).DoStuffPlugin();
});
This wreaks havoc with some object oriented programming techniques that uses this
for the object reference:
function ClassSmurf(){
this.name = "smurfette";
this.SmurfAround = function(){ alert("Smurf: "+this.name); }
};
This is a simple example broken by jquery’s use of context (that jQuery binds this). There is a simple work around:
function ClassSmurf(){
this.name = "smurfette";
var that = this;
this.SmurfAround = function(){ alert("Smurf: "+that.name); }
};
Note that we capture the object instance as a private variable. We do get some additional boilerplate in order to have jQuery safe “classes”. Don’t take me wrong. I love that we have jquery.
Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.
Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.
Comments