Archive for September, 2011

Mistakes On Chain Assignment In javascript

Sep 30, 2011 No Comments by Mahdi Pedram

One common mistake that I see developers do is exposing a variable to the Global object in chain assignment.
first I let’s see how can you expose a variable to the Global object by accident.
 
function test(){
a =12; // var is missing
return a;
}
test();
 
By missing the keyword var even though variable a is inside of the function but [...]

javascript Read more

Namespace in jQuery

Sep 27, 2011 No Comments by Mahdi Pedram

I have implemented a jQuery plugin that will take care of namespacing for us.
if you don’t know what is namespace please read it from here
take a look at the code below. and see how it works. you can get the code from jsfiddle as well
$.namespace("liteframe.actions.controller");
//OR [...]

Uncategorized Read more

Removing elements from an array in javascript

Sep 27, 2011 No Comments by Mahdi Pedram

If you are trying to remove elements from an array. and do not care what those elements are, you shouldn’t use array slice, array splice.
however there is an easier way of doing this.
by just changin the length of an array we can modify our array and remove items from it.
 
var arr = [1,2,3,4,5];
arr.length = 2;
console.log( [...]

javascript Read more

Repeating a string without using a for loop in javascript

Sep 27, 2011 No Comments by Mahdi Pedram

Even though it is not recommended to use Array constructor. but sometimes we can get some usage of it.
One good example would be repeating a string without using a for loop.
in this example we are creating an empty array with default undefined values.
( when you specify a size for an array, without setting the values [...]

javascript Read more

Securing Undefined Variable In Javascript

Sep 26, 2011 No Comments by Mahdi Pedram

Sometimes some bad programmer or some hacker messes up with the undefined global variable in javascript.
so why would we really care about it.
the following are some examples that the variable undefined will be returned
 
 
var myVar = undefined; //Anything that has been set to the value of undefined.
 
function test(){}; console.log( test() ); // returns undefined – [...]

javascript Read more

Type Casting In Javascript

Sep 26, 2011 1 Comment

I see a lot of confusion among the javascript developers when it comes to casting in javascript.
Casting To A Number
let’s say you have a String and you want to convert it to a Number. one would say I will use parseInt() or parseFloat().
but there is even an easier way of doing it. no need to [...]

Read more