Friday, April 10, 2015

Do not declare function and variable with the same name in same scope

In Javascript functions and variables share same namespace  and that is why this code will break

 

<script>

        function ThisIsAFunction(n) {

            alert(n);

        };

 

        var ThisIsAFunction = 'India';

 

        alert('abount to execute');

 

        ThisIsAFunction('Hello');

    </script>

 

Basically 1st statement in code is similar to

var ThisIsAFunction  = function ThisIsAFunction(n) { alert(n); };

 

Or

 

var ThisIsAFunction = = function (n) { alert(n); };

 

and this will collide even inside a closure as closure will be scope rather than global.

5 comments:

Nandhini said...

Pretty section of content. I simply stumbled upon your site and in accession capital to say that I get actually loved to account your blog posts.
Security Services in Chennai

Abiya Carol said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

AWS Training in Chennai

Mahalyasree said...

This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.
Office Interiors in Chennai
Interior Decorators in Chennai

Sowmiya said...

Really Good blog post.provided a helpful information.I hope that you will post more updates like this.
MSBI Training in Chennai

Alya Kannan said...

very very amazing explaintion....many things gather about yourself...yes realy i enjoy it
SEO Company in India