Pre-cache your for loops

While reading through the JavaScript Patterns book, I saw an item that totally stuck out as a crystal clear “duuhhh!!” moment. Why have I never seen/heard of that before and certainly why wasn’t I doing it regularly (and why hadn’t I thought of it to be honest). Instead of doing this: [sourcecode lang=”javascript”] for(var i=0; i < $(‘someselector li’).length; i++){ //loop through each LI in a UL, or any collection [Read More]

JavaScript books, recent projects, Line rider!, Rock climbing

I went on a bit of a shopping spree on Amazon. Besides a few Kindle books (sci-fi, etc) I grabbed a few new JavaScript books as well: JavaScript Patterns – Stoyan Stefanov Secrets of the JavaScript Ninja – John Resig, Bear Bibeault If you are looking to utterly destroy an entire afternoon (or more), definitely check out the HTML5 version of an oldie but goodie, Free Rider HD!! (also know as Canvas Rider) [Read More]

Use “debugger;” in your JavaScript to trigger the browsers debug mode.

While watching the excellent course on Single Page Apps with HTML5, Web API, Knockout and jQuery by John Papa over at PluralSight, he dropped a nice little nugget of information that I was totally unaware of. You can add debugger; as a line by itself in your JavaScript and when your code gets executed by the browser, the browsers built in debugger will stop on that line. Think of it as a way to throw breakpoints right into your code. [Read More]

Wrap your custom JavaScript code in Closure functions

I finally figured out how to use closures properly in JavaScript. This came as a result of my needing to make sure globals I was defining in different script files weren’t conflicting with same named globals in other files. My solution was rather simple, just wrap existing chunks of code in their own function wrapper: Original Code: Note that the variables globalVar and anotherGlobal are truly GLOBAL and any other javascript file(s) with the same variables declared will cause major issues for our code (or vise versa). [Read More]

JavaScript == is != to ===

Watching JavaScript fundamentals (Pluralsight) as a refresh and had my world rocked – apparently we should be using === and !== all this time and not == and != that’s just nutty!!

Basically “” == 0 (true), “” === 0 (false), 1 == “1” (true) 1 === “1” (false).

MVC postback bug, Underscore.js, ===, Asana, Launched FillThePart.com!!, Visual Studio 2012

After being stumped for a while with a particularly interesting issue in MVC – I found the solution on Rick Strahl’s blog. Specifically I was trying to change the values of a view model from a controller action, after PostBack – yet my changes seemed to be ignored or simply refuse to stick. Turns out the HtmlHelpers refer to the actual POST data on page load after postback (seemingly ignoring the “model” even though you explicitly refer to the model when using HtmlHelpers). [Read More]

Use ?? to set value if something is NOT null

Learned to use the ?? operator when checking for null.  Use when you want something to = something else only when something else is not null:

var myvar = something_else ?? “default_value”;

myvar will be set to something_else only if something_else is not null, otherwise it will be “default_value” instead.