Smart javascript arrays
September 16th, 2008 Filed Under Good to know, Javascript, Tips & Tricks, gyan, snippet, vivek
Today I want to show you some really professional and efficient way to populate your javascript array.
When you declare an array you can do it simply by writing a short hand like:
var arrSalaries = [];
Notice that the square brackets[] itself delcares the array and you don’t have to write “new Array()”. So you save some characters like that. Thanks to javascript.
Sometimes you need to populate an array in a loop and generally people populate it like this:
for(i=0; i<something.length; i++)
{
arrSalaries[i] = something[i].amount;
}
There is nothing wrong in the code but a more robust way would be:
for(i=0; i<something.length; i++)
{
arrSalaries[arrSalaries.length] = something[i].amount;
}
This ensures that any exceptions(if any), would be taken care and the array would be properly populated. To be in simple words, let us say you have an exceptional continue statement in the for loop and the statement is executed on some event. In such a case, the array would be populated wrongly in the following fashion:
arrSalaries[0] = 200; arrSalaries[1] = 200; arrSalaries[3] = 200;// just now there was the exception arrSalaries[4] = 200; arrSalaries[6] = 200;// just now there was the exception
If you use the second method to populate the array you can avoid this problem.
Another thing in array(not everyone remembers) is the literal arrays.
So you can delcare an array with a literal like this:
var arrSalaries = []; arrSalaries["vivek"] = 5000; arrSalaries["shon"] = 5000;
And you can access them with a dot operator like:
alert(arrSalaries.vivek);
or the same way you declared it:
alert(arrSalaries["vivek"]);
One should always think to minimize the code as far as possible. Even a single character shows your optimization attitude.
Now after knowing this one obvious question comes to your mind that how to loop through this literal arrays:
The answer is the following code:
for(props in arrSalaries) //in case you have forgotten for ... in in javascript this is a reminder
{
alert(arrSalaries[props]);//props represent the literal
}
The above way can be implemented to dig out any built-in or custom object to scan their attributes and methods.
But as commented by Andrew Dupont in his article associative arrays should not be used for storing values with literals at all.
Andrew is technically correct in saying that because you loose all the basic methods in this kind of array.
Better use it like this:
var arrSalaries = {};
arrSalaries["vivek"] = 5000;
arrSalaries["shon"] = 5000;
So, what you replace is [] with {}. And hence, you come to know that {} is a shortcut of declaring an object. Enjoy madi !!
Post Linx
Permalink | Trackback |
|
Print This Article | 2 Comments
Google Chrome Shortcuts
September 9th, 2008 Filed Under Good to know, browser, shortcuts
About Commands
about: ->show version info
about:version -> same to about:
about:cache -> show cache content
about:plugins -> show info of plugins installed
about:memory ->show memory usage
about:crash -> crash the tab
about:dns -> show dns info, like time
about:network -> network tools
about:stats -> shh! this page is secret!
about:internets -> the tubes are clogged!
about:histograms ->histograms resume
Window and tab shortcuts
Ctrl+N Open a new window
Ctrl+Shift+N Open a new window in incognito mode
Press Ctrl, and click a link Open link in a new tab
Press Shift, and click a link Open link in a new window
Alt+F4 Close current window
Ctrl+T Open a new tab
Ctrl+Shift+T Reopen last tab closed (remembers 10)
Drag link to tab Open link in specified tab
Drag link to space between tabs Open link in a new tab
Ctrl+1 through Ctrl+8 Switch to the tab at the specified position number. The number you press represents a position on the tab strip.
Ctrl+9 Switch to the last tab
Ctrl+Tab or Ctrl+PgDown Switch to the next tab
Ctrl+Shift+Tab or Ctrl+PgUp Switch to the previous tab
Ctrl+W or Ctrl+F4 Close current tab or pop-up
Alt+Home Open your homepage
Ctrl+O, then select file Open a file from your computer in Google Chrome
Address bar shortcuts
Do one of the following actions in the address bar:
Type a search term Perform a search using your default search engine
Type the part of the web address that’s between ‘www.’ and ‘.com’, then press Ctrl+Enter
Type a search engine keyword or URL, press Tab, then type a search term Perform a search using the search engine associated with the keyword or the URL. Google Chrome prompts you to press Tab if it recognizes the search engine you’re trying to use.
F6 or Ctrl+L or Alt+D Highlight content in the web address area
Type a web address, then press Alt+Enter Open your web address in a new tab
Shortcuts to open Google Chrome features
Ctrl+B Toggle bookmarks bar on and off
Ctrl+H View the History page
Ctrl+J View the Downloads page
Shift+Escape View the Task manager
Webpage shortcuts
Ctrl+P Print your current page
F5 Reload current page
Esc Stop page loading
Ctrl+F5 or Shift+F5 Reload current page, ignoring cached content
Press Alt, and click a link Download link
Ctrl+F Open find-in-page box
Ctrl+G or F3 Find next match for your input in the find-in-page box
Ctrl+Shift+G or Shift+F3 Find previous match for your input in the find-in-page box
Ctrl+U View source
Drag link to bookmarks bar Bookmark the link
Ctrl+D Bookmark your current webpage
Ctrl++ Make text larger
Ctrl+- Make text smaller
Ctrl+0 Return to normal text size
Other
Backspace, or press Alt and the left arrow together Go to the previous page in your browsing history for the tab
Shift+Backspace, or press Alt and the right arrow togetherGo to the next page in your browsing history for the tab
Ctrl+K or Ctrl+E Places a ‘?’ in the address bar. Type a search term after the ‘?’ to perform a search using your default search engine.
Place your cursor in the address bar, then press Ctrl and the left arrow together Jump to the previous word in the address bar
Place your cursor in the address bar, then press Ctrl and the right arrow together Jump to the next word in the address bar
Place your cursor in the address bar, then press Ctrl+Backspace Delete the previous word in the address bar
Space bar Scroll down the web page
Home Go to the top of the page
End Go to the bottom of the page
Post Linx
Permalink | Trackback |
|
Print This Article | Leave a Comment

