LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2013, 05:58 AM   #1
S. Chapelin
Member
 
Registered: Jan 2010
Location: Quebec
Posts: 119

Rep: Reputation: 5
Can you explain to me some strange javascript code?


I am playing around with HTML5 and javascript and have found a file called
Quote:
Hand.js: a polyfill for supporting pointer events on every browser
Being a linuxian, I am naturally curious how the code works. I have encountered at the beginning of the file this bit of voodo:
Code:
var HANDJS = HANDJS || {};
(function () {
// Code to the end of the file
})();
Can someone explain to me how these lines work and what they do?
It is seemingly a function enclosed in parentheses followed by parentheses. Why?
What is that weird name: function()? Shouldn't function be a reserved name?
I presume that the first line puts true or false into the var HANDJS so that the file is not loaded twice.
 
Old 11-09-2013, 06:31 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,136

Rep: Reputation: 230Reputation: 230Reputation: 230
Hi

The first line sets HANDJS to an empty object if the value evaluates to 0. This is usually done to set it if the value is null. Parameters to a function will get a value of null if they're not spcefied, so code like this is usually used to set default values.

The rest is an anonymous function. They're usually used as callbacks to other functions, like this:

setTimeout(function(){alert("Hello")},3000);

But here it's just defined and called. (Notice the last () which means call that anonymous function). Any variables inside that function will be local when prefixed with "var", so it won't pollute or overwrite other variables with the same name. The purpose is to get a new namespace.
 
Old 11-09-2013, 12:25 PM   #3
dugan
Senior Member
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,676

Rep: Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441
Quote:
Originally Posted by Guttorm View Post
The first line sets HANDJS to an empty object if the value evaluates to 0. This is usually done to set it if the value is null. Parameters to a function will get a value of null if they're not spcefied, so code like this is usually used to set default values.
You meant "undefined", not "null", right?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python : Will any one explain me this code, please ? aarsh Programming 6 04-03-2011 09:37 AM
Could someone help explain this code? MTK358 Programming 27 04-01-2010 09:44 PM
help to explain the shell code ! nillgump Linux From Scratch 1 10-11-2008 11:09 AM
explain me some c code alaios Programming 4 11-28-2005 11:27 AM
explain me this c code plz alaios Programming 1 06-03-2005 05:32 AM


All times are GMT -5. The time now is 07:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration