• Designs Photo
  • Fantasy Photo
  • Code Photo
  • Silhouette Photo

Creative, Modern, Professional Designs

For the last 6 years, I have been creating innovative websites to capture attention and inspire the mind, balancing user experience with an attractive website interface. By the use of AJAX and CSS, websites become more interactive, and with Search Engine Optimization, they are easily found through Google, Yahoo and Bing.

Designs Photo
Fantasy Photo

Everything you see here is Original Work

Besides the jQuery library and syntax highlighting, this whole website has been created from scratch; No pre built frameworks or content management systems have been used.

This demonstrates dedication, personalization and skill, and is reflected in all of the work I produce.

Programming is a Passion

As you get to know me, you'll start to see that I've tried my hands at just about everything, ranging from the basics to the more advanced and finer points in programming.

That means you'll probably find lots of hints and tips here for many different languages, as well as anything that I find relatively interesting!

HTML Code Photo
Silhouette Photo

Inspiring Designs with Limitless Possibilities

Every website created here is a unique one, with limiting boundaries out of the questions.

Programming is always evolving and so I'm always improving my game to keep up with the changes. You'll be able to see this reflected in the high quality work that I produce.

Anuj Nair's Blog »

jQuery Object Literal Notation and the this keyword

How to avoid jQuery overwriting the scope of the 'this' keyword

Posted: 26 February 2011 - Written by Anuj Nair
Whilst using jQuery, I've been using the Object Literal Notation to namespace all of my JavaScript code:

$(document).ready(function() {
	var example = {
		var1: 1,
		var2: 2,
		someFunction: function() {

		}
	};
});


In my function, I wanted to use a jQuery effect, and in that callback, I wanted to reference the variables var1 and var2. Something like so:

$(document).ready(function() {
	var example = {
		var1: 1000,
		var2: 2000,
		someFunction: function() {
			$('#someId').fadeOut(var1, function() {
				alert(var2);
			});
		}
	};
});


The problem with trying something like this, is that you have to use the "this" keyword to reference the variables. However, when you enter the callback function of the jQuery function, the "this" scope is overwritten by jQuery itself, and so you can no longer reference the second variable.

To solve the issue, you simply have to save the scope of the initial "this" and use it in the callback like so:

$(document).ready(function() {
	var example = {
		var1: 1000,
		var2: 2000,
		someFunction: function() {
			var self = this;
			$('#someId').fadeOut(self.var1, function() {
				alert(self.var2);
			});
		}
	};
});


Here, I've saved the "this" scope into the variable "self", and the code now works.
Comments
No one has posted any comments yet; Why not be the first?
Name :
Email (hidden) :
Website (optional) :
Captcha :
 
How to Post   Comment :