Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Archive for July, 2009

Jul 29 2009

Readings

Published by under Links


Comments Off

Jul 28 2009

Readings

Published by under Links

Most of the time, I have been sharing the links that I found interesting via Twitter and Facebook but, from now I will be posting them as a blog post so that people subscribing and reading my blog can view all the links I’m sharing in one place at same instance. I did use Delicious to do similar task in the past but it just didn’t work due to various reasons. The links post won’t be more than one per day hopefully and I also hope that you will get some extra information from them :)

Here are the links for today:


Comments Off

Jul 21 2009

Global Variables in Javascript

Published by under JavaScript

Normally, global variables are considered evil but in Javascript they are still used considerably.

var test = "test";
console.log(window.test == "test");

Okay, this one is usual and expected.

if (true)	{
var test = "new test";
}
console.log(window.test == "new test");

Declaring the same variable inside the expression also gives any variable global scope.

function smth()	{
var test = "no new test";
}
smth();
console.log(window.test != "no new test");

When the same variable is declared using ‘var’ keyword inside a function expression, it won’t have global impact.

function smth2()	{
test = "new new test";
}
smth2();
console.log(window.test == "new new test");

But when the same variable (test in our case) is used inside a function expression, it will continue it’s global effect.

function smth3()	{
test2 = "new variable";
}
smth3();
console.log(window.test2 == "new variable");

This case is a bit interesting and odd. Even though, test2 has not been declared earlier, it will be treated as global variable now.

Hope this help!


Comments Off

RSS Feed
Subscribe by email
Follow me @ Twitter