Contents tagged with javascript

  • JavaScript Date Tips

    The other night at the Hudson Software Craftsmanship meeting at the Falafel Software training center in Hudson, Ohio, I did the Red Pencil Kata using JavaScript.  Although I’ve run into it in the past, I was stuck for a little while (I was the odd man out without a pairing partner to help find these things faster) due to one of JavaScript’s “fun” date conventions.  Being a C# developer primarily, there are many small things I have to remember that are different between JavaScript and C# (like where to declare variables, for instance).  In this case, it was the code for generating a particular date that bit me.  At one point, I wanted to generate a … more

  • Where to Declare Variables in C# and JavaScript

    Both JavaScript and C# belong to the C family of languages.  They share curly braces and semi-colons, and in fact there are many cases where the exact same code will execute (correctly, in most cases) as either language.  However, there are certain best practices that are unique to each language, and where variables should be declared is one of them.

    Declaring Variables in C#

    In C#, it’s generally best to declare variables just before they’re used.  Like any convention in programming, there’s some debate about this, but this rule is supported by well-respected books like Clean Code and top-voted answers to questions like this one: Where do you declare … more

  • Ensure You Are Not Adding To Global Scope in JavaScript

    A key best practice if you’re writing JavaScript code is to avoid adding objects to the global scope.  There are several good reasons for this – globals add coupling, it makes it easier for disparate libraries to break one another, etc.  A general rule of programming is to avoid global scope, in fact.  Unfortunately, JavaScript makes adding things to global scope very easy.  Consider this bit of code: In this example, we have a function, within which we set a variable and then use some jQuery to modify a div.  However, because we didn’t specify the “var” keyword, we’ve actually just added a new object to the global scope: message.  In fact, if some other … more

  • Working with Kendo UI Templates

    I’m watching the Introduction to Kendo UI course by Keith Burnell on Pluralsight and decided to play around a little bit with templates, which are a pretty powerful feature.  If you’re a server-side web developers, templates should seem pretty familiar to you.  Any ASP or ASP.NET page can be thought of like a template, in which you mix markup and dynamic data expressions.  In this case, the templates are defined in their own script blocks.  There are demos and documentation available; in my case I opted to write something up based on what’s in the Pluralsight course in a JSFiddle. Templates are most valuable when you’re binding some data to them.  The data can be … more

  • Passing Default Parameter Objects in JavaScript

    You can use the jQuery extend() function to elegantly configure your functions to accept a single parameter object, while providing default behavior for any options that are not set.  For instance, imagine you have a simple function that simply says “Hello, World”.  You could code it like this: Now, if you want to start passing in parameters, you can easily do so by parameterizing the method, but every time you do, you break any code that was expecting the original parameterless version.  This can quickly grow out of hand within a few revisions of the function’s signature.  A simple way to avoid this issue and allow for your function to version forward effectively is to … more

  • Wiring Up TimeAgo and ASPNET MVC

    Imagine you want to display something on a page so that instead of raw dates, the user is shown something more relative to the current time.  You’ve probably seen this in some of the applications you use.  “Last Updated: A moment ago” or “about an hour ago”.  There are a variety of ways you can implement this, and it’s been done in many different languages.  In my case, which is an ASP.NET MVC C# application, I considered doing it on the server and simply passing the result as a string to the view for display.  However, in searching for the best way to do this, I stumbled upon the Timeago plugin for jQuery. One of the key benefits of this plugin is that, unlike … more

  • JavaScript The Good Parts Reviewed

    Finished up Douglas Crockford’s JavaScript: The Good Parts this week.  It definitely helped me improve my understanding of JavaScript, which I’ve been using since it was new, but always like a C programmer, and only recently like a JavaScript programmer.  I really appreciated Crockford’s honest, no-holds-barred analysis of JavaScript’s design and language choices.  It had me chuckling more than once.  I also appreciated that the book, at only 150 pages including the index, is devoid of fluff.  There’s just enough repetition between chapters to ensure certain points are made and made well. The book makes frequent use of railroad diagrams, which I’ve seen before but … more