Monthly Archives: October 2013

GRUNT – JavaScript Build Tool For Everyone

It doesn’t matter if you are a high level developer or

grunt

a newbie – Grunt can help you. I will share a few practical ways

Minify your codethat I use Grunt, although I can for sure do a lot more with Grunt than just those solutions that I will discuss I will only discuss only a few of my favorites. Also I am still in the learning process of all of the benefits of Grunt, so if you have a usage that you think is important and is not listed here, please share! All of my current usages for Grunt is for static resources.

With Grunt you can easily minify your code into a destination file. The most common way to minify a file with Grunt is to use Uglify.js. there is a lot of information on the Grunt website on how to get started.

Combine a few JavaScript files

With Uglify you can combine a few files into one file – that is great for configuration files. If you have different configuration values on your development server than your production server – just make all your configurations on a separate file and combine it with Grunt. The build will be per server.

There are a lot of ways to handle configuration in JavaScript. The most obvious is to just put it in the script without thinking about it too much. That solution is good only if you test everything on production, but for those sane developers like us that want to test our scripts on multiple environments we need a good way to easily move our scripts from one environment to the other. With Grunt it is easy to do so – use a settings file that is combined during the build process

JSLint

you can put JSLint as part of the build process, personally I don’t use that feature since I use JSLint directly into my IDE, but that can be useful for other people.

Testing

We can integrate our tests with the Grunt build. This is extremely useful since I can integrate the testing process directly to the build process and when the test is failing – I will get an immediate feedback.

Reformatting code

remove all of your debugging code on the build, that way no accidental console.log statements on the real script.

How to install?

A great in depth manual is in the Grunt website. It is depended on Node.js.

Getting to Know JavaScript Scoping

js-150x150Becoming a Pro in JavaScript requires a few capabilities, one of the most important ones, is understanding JavaScript variable scoping. A lot of developers that came from a different programming language bang their head in the wall often until they fully understand JavaScript scoping mechanism. Let’s start:

What is Scoping?

scope-200x300You can’t access everything from anywhere. Each function and variable in JavaScript has a scope, you may have not been aware to the existence of the scope, but it does exist. A scope is basically an access mechanism to an object, function or variable. if you can access an object than you are in the object scope, and If you have no access, you are out of scope. There is a scope that is accessible from anywhere – the global scope. but let’s first start with the simple scope mechanism – the function scope.

Function Scope

In JavaScript – the scope is changed only in functions and not in any curly braces {..} like in a lot of other languages. so if we look at the next example we can see that we can’t access the ‘bar’ variable since we try to access it outside the function and that means that we try to access a variable out of the scope and that is not possible so the alert will throw a “ReferenceError: bar is not defined”.

The function ‘bar’ has it’s own scope and anything that will be defined in the function could not be accessed outside of the function.

Global Scope

The global scope can be accessed from everywhere, as a rule of thumb there should be as few global scope variable as possible. A lot of global variables can cause conflicts in complex applications.

Access from everywhere means that you can access the global scope from any portion of your code, at any time. When a variable is scoped globally it can also be accessed through the ‘window’ object, I mention that because of the next example:

There are mainly 3 ways to declare a variable in the global scope:

  • Each variable that will be declared in the main script, that means not inside a function will be scoped in the global scope

  • Declaring a variable without using the ‘var’ keyword will cause the variable to be scoped globally, no matter where it was declared. that is a huge pitfall for a lot of beginners.

  • Adding a property directly to the ‘window’ object - window.foo = “hello”

From the above example we can learn that although the scoped var foo in the function is now “hello” the original foo variable is still accessible through the window object who holds all the global variables.

As I said before, any variable or function that is declared outside a function or object will be scoped automatically in the global scope.

Always Declare your Variables

When declaring a variable – it should always be prepended with “var”, not doing this will cause the variable to be scoped globally. that can cause an unexpected behavior.

Look at the next example in order to understand how forgetting to prepend a variable with the ‘var’ keyword will cause the ‘bar’ variable to be scoped globally :

To better understand the problem that could arise from this, take a look at the next example:

because ‘i’ is used in foo in the global scope, the ‘var’ was not used… than the alert will not say that “i is true” because ‘i’ is actually equals to zero because we called the foo() function before our if.

so to summarize how you should use any var in a function, don’t use it in a global scope like that:

You might think it is better to do it like this:

but you will be wrong, well, at least not 100% right..
The best way is to declare it at the beginning of the scope:

 

but… why? well, it is the preferred way because of what is called Hoisting.

JavaScript Hoisting

Hoisting is a strange behavior when you first hearing about it, what it means is that any variables / function that belong to the current scope will be declared at the beginning of the scope. the difference between a variable and a function hoisting is that the function will have a value in the beginning of the scope while a variable will be undefined.

Why is that something that is important to know? take a look at the next example:

you would think that the output will first log ‘hello’ and ‘world’ but it will actually will log ‘undefined’ and ‘world’.
why? Hoisting! that is why… with hoisting your code will actually be interpreted as:

So in order to not be surprised with unexpected behavior – it will be a good idea to always declare any variable that is used in the scope at the beginning of the scope.

Unlike variables, functions have their value when called before their declaration. You can see that from the last example where we called the ‘bar’ function before it was declared, That was possible because of hoisting that was interpreted into:

Conclusion

Scoping in JavaScript can be tricky. but all the common pitfalls can be avoided with the coding conventions I presented – declare all scoped variable at the beginning of the scope and prepend all variable declaration with the ‘var’ keyword.