I found an article detailing some “oddities” in Javascript today. The callback argument in the replace method was new for me, though I feel that there must be some eccentric behavior that pushed this “secret” into the shadows.

Number 7 proved the most beneficial for me. My attempts to pass the current value of a variable instead of the variable itself have long been thwarted time and time again, and I eventually resorted to using a separate variable to hold the value I needed. This is okay for a single instance, but when I’m plotting hundreds of places on a map the overhead could get cumbersome (caveat: storing each map marker in a separate object has other benefits, so maybe this is a poor example). Anyway, now I see how to “isolate” the value that is passed to a function:

var someVar = 'hello';
setTimeout((function(someVar) {
	return function()  { alert(someVar); }
})(someVar), 1000);
var someVar = 'goodbye';

This function will alert “hello” – the value is passed to the function instead of the variable. Thanks, Smashing, for another great post!