<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-9018206052194537874</atom:id><lastBuildDate>Fri, 30 Aug 2024 10:08:33 +0000</lastBuildDate><category>java</category><category>STRUTS 2</category><category>javascript</category><category>jQuery</category><category>NAKED JAVASCRIPT</category><category>TOOLS</category><category>PATTERNS</category><category>classpath</category><category>CHROME</category><category>css</category><category>GITHUB</category><category>J2EE</category><category>JSF</category><category>ORACLE</category><category>AJAX</category><category>DATABASE</category><category>ECLIPSE</category><category>MySQL</category><category>OPEN SOURCE</category><category>Productivity</category><category>SERVERS</category><category>SNIPPETS</category><category>SWING</category><category>TODO</category><category>TOMCAT</category><category>drupal</category><category>html</category><category>tips</category><title>MyCodeFixes</title><description>The Annals Of My Experiments :&amp;gt;</description><link>http://mycodefixes.blogspot.com/</link><managingEditor>noreply@blogger.com (Anonymous)</managingEditor><generator>Blogger</generator><openSearch:totalResults>103</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-4958365717072278691</guid><pubDate>Sun, 06 Apr 2014 00:58:00 +0000</pubDate><atom:updated>2014-04-06T06:28:52.137+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Free Windows Partition and Backup Tools</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
It’s been eons since I posted on this blog. Grad school kept me pretty busy. In the meanwhile I also created another blog that mainly deals with my thoughts on user experience and HCI. Anyways, with my graduation date almost around the corner, its time to resume writing at this blog as well.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Since this is almost as good as a fresh start, I just going to keep it short and write about 2 pretty useful tools that as a windows user you probably want to keep in your arsenal.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The first is &lt;a href=&quot;http://goo.gl/3Psose&quot; target=&quot;_blank&quot;&gt;EaseUS partition master&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If you ever need to create partitions on your windows hard drive, because the C drive is just lame if everything is just in that one single drive, this is the tool to use. Best of all, it is free for personal use. So, just do yourself a favor and grab this one.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Then next one is &lt;a href=&quot;http://goo.gl/cvmg3D&quot; target=&quot;_blank&quot;&gt;SyncBackFree&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As the name suggests, it lets you sync data across drives. If you are used to using &lt;a href=&quot;http://goo.gl/iTCFKC&quot; target=&quot;_blank&quot;&gt;WinSCP&lt;/a&gt; when designing websites to keep the files on your local and remote server in sync, you will find this tool extremely pleasant. This is what I use to keep a mirror image of my windows drives on my portable drive.&lt;/div&gt;
&lt;br /&gt;
Signing Off&lt;br /&gt;
Ryan&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2014/04/free-windows-partition-and-backup-tools.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-5364388400232694264</guid><pubDate>Sun, 30 Dec 2012 01:03:00 +0000</pubDate><atom:updated>2012-12-30T06:33:16.819+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">DATABASE</category><category domain="http://www.blogger.com/atom/ns#">MySQL</category><title>Backup and Restore using MySQL Workbench</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Not so recently, I installed MySQL Workbench on my local system for a personal project. I had installed it a long time ago and it seems that after Oracle&#39;s acquisition of MySQL, they have made quite a few changes to the setup. Especially the bundle that you can install now contains examples and MySQL server and the MySQL workbench.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;One nice feature about this new bundle is that you don&#39;t need to install additional software if you already have MySQL workbench installed. Earlier, you had to install MySQL admin to do a backup and restore of your system. Now its a bit more simplified since everything is accessible from within MySQL workbench itself.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In this post, I am just going to show the steps involved in using the new MySQL workbench (the one that I have is MySQL Workbench 4.2.44 CE) to take a backup of your database in a simple text file and then later use the same text file to restore your database to a prior state.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;My MySQL database is on my local system, uses the standard port 3306 and is at its default install state at the moment. The screenshot of the homepage looks like this.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkhhClGnDgXaRBSvdkN4cl7vnZPEEVnjKgiMSDa4xHTtdo2mrYK93puetCaQZPlhsxqIWBsdHKZY1Cp8rbIXCQ9hk-LTrRVwqIJ9aq_bvTk5HVmIx5KSzIrYThcGxaBz8Slr7x7gViU0Q/s1600/0_Home_Page.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;276&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkhhClGnDgXaRBSvdkN4cl7vnZPEEVnjKgiMSDa4xHTtdo2mrYK93puetCaQZPlhsxqIWBsdHKZY1Cp8rbIXCQ9hk-LTrRVwqIJ9aq_bvTk5HVmIx5KSzIrYThcGxaBz8Slr7x7gViU0Q/s400/0_Home_Page.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In order to take a backup of your database, you need to focus on the Server Administration section of Workbench and select your MySQL server from the list of servers displayed.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Once you do that, you are taken to a page where it shows you details about the health of the server and the different operations that you can perform on the this instance of your MySQL server.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhojgfX9RPsjCQU353qkxCRilpoTZ_RyB3Kk775cnbUnS9Wl-ygqhfFXszvawe2-c_RCjeUkbDaflJBB-m37s-LpxPD2n5x3J6SHgw5QSJ-H4rLpmjI0c6NxCGwBVU0cFIGIAhxS-ncZY4/s1600/1_myslq_admin_home.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;278&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhojgfX9RPsjCQU353qkxCRilpoTZ_RyB3Kk775cnbUnS9Wl-ygqhfFXszvawe2-c_RCjeUkbDaflJBB-m37s-LpxPD2n5x3J6SHgw5QSJ-H4rLpmjI0c6NxCGwBVU0cFIGIAhxS-ncZY4/s400/1_myslq_admin_home.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Since you want to take a backup, you need to select the option that is circled in red that lets you import and export data.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Lets first export our data. Its pretty easy, select the database and then the tables from that database of which you want to take a backup.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA68hgFeCr7vc598a25UpITqS7hEAvRifL74Qwz3r29YJprwUoL7Moiq2CsAa_d-XIbysXvsOB3dzalTiQBjCepet6FhuSCOozZFf_WfBKQEutq7kV-S3La74yUjs6Huhyphenhyphen8i-etLMOPTY/s1600/2_table_selection.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;277&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA68hgFeCr7vc598a25UpITqS7hEAvRifL74Qwz3r29YJprwUoL7Moiq2CsAa_d-XIbysXvsOB3dzalTiQBjCepet6FhuSCOozZFf_WfBKQEutq7kV-S3La74yUjs6Huhyphenhyphen8i-etLMOPTY/s400/2_table_selection.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;For small projects, its convenient to take a backup in a single file. This file will contain all the sql statements that can be used to backup all the data and structure of you schema. Most of the times, you might want to take a backup of only the data, but not the schema. If you are taking a backup of the schema, it just means that when you use the file to restore, it will try to recreate the tables if the they don&#39;t already exist. However, if you don&#39;t want this extra detail to be present in your backup file, and only want backup of the data, an option in the advanced tab lets you do that.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJSzBCHtZBnteu8PMtRIhrWVX6hy9LJRQhXOBP4S8kGALRiksSJxkJMKo9s5K9alzkhxzo4NkVHfVqugSvKhrfeN1xd41iaagoOW6QVk_ZNstouluVPWYM5ye8hlJAt5tDRX43oYHZlhs/s1600/3_unselect_schema_creation.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;290&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJSzBCHtZBnteu8PMtRIhrWVX6hy9LJRQhXOBP4S8kGALRiksSJxkJMKo9s5K9alzkhxzo4NkVHfVqugSvKhrfeN1xd41iaagoOW6QVk_ZNstouluVPWYM5ye8hlJAt5tDRX43oYHZlhs/s400/3_unselect_schema_creation.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Once &amp;nbsp;you are done mixing and matching all the options, click on the Export progress tab and then click on Start Export.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUo5F3QFz33bcQ7mTNn5E9I18ec5WV1QdOYayKoO4dUN0jDyJOkZF392aDwETMyDCvH6o7ALjywccijxfLphZaiZJgb0dsrs-wq_3dFA68vOl8qlNBWqa73V5m7kCHKJADsR5ndWSg9yc/s1600/4_export.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;287&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUo5F3QFz33bcQ7mTNn5E9I18ec5WV1QdOYayKoO4dUN0jDyJOkZF392aDwETMyDCvH6o7ALjywccijxfLphZaiZJgb0dsrs-wq_3dFA68vOl8qlNBWqa73V5m7kCHKJADsR5ndWSg9yc/s400/4_export.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;That&#39;s as easy as it gets to take a backup of your MySQL database.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Restoration is an equally easy process. Just select the file that you want to use as the restoration file and click on start import. And you are good to go.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGPbw0KUY_ePLq1GInGKIy9ZV6VkzbAxlikItBLCMwmCrFHAgGaLPGD0iuiiCAc6XXLY0rBAANd3B7AUtlvM-gkdbPUYjZbdmkA9do1U5phcB5gTJIq6SdU2HdRpqM2L6hRsWrXYImfZw/s1600/5_restore.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;285&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGPbw0KUY_ePLq1GInGKIy9ZV6VkzbAxlikItBLCMwmCrFHAgGaLPGD0iuiiCAc6XXLY0rBAANd3B7AUtlvM-gkdbPUYjZbdmkA9do1U5phcB5gTJIq6SdU2HdRpqM2L6hRsWrXYImfZw/s400/5_restore.png&quot; width=&quot;400&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Pretty neat, although I do believe a few more changes to the interface could make some things more obvious.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Hope this helps!&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Happy Programming :)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Signing Off&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Ryan&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/backup-and-restore-using-mysql-workbench.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkhhClGnDgXaRBSvdkN4cl7vnZPEEVnjKgiMSDa4xHTtdo2mrYK93puetCaQZPlhsxqIWBsdHKZY1Cp8rbIXCQ9hk-LTrRVwqIJ9aq_bvTk5HVmIx5KSzIrYThcGxaBz8Slr7x7gViU0Q/s72-c/0_Home_Page.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-8453082952179070141</guid><pubDate>Wed, 19 Dec 2012 21:43:00 +0000</pubDate><atom:updated>2012-12-20T03:26:37.982+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">GITHUB</category><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Git : Editing A Previous Commit</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you work with Git, making branches is a very common task and when you are flooded with ideas, you &amp;nbsp;may be in a hurry and sometimes make mistakes. The most common example of making mistakes when committing in git is - the commit message. Sometimes, just after committing, you would realize that you forgot to mention something crucial.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Sometimes, it may also happen that when you create your commit, you might accidentally forget to add a particular file to the commit. Even this is a likely scenario since git does not automatically add new directories under version control when you add them through your local file system. So if you did a git add . during your first commit and then created lots of subdirectories and files, they will not be added automatically just because you had done an initial &quot;git add .&quot;.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Well, there is one small source of relief - and that is - you can easily amend the last commit in git and fix these tiny issues. For example, let us say that you just committed with a very nonsensical and convoluted message. like this&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;git commit -m &quot;I was so drunk when I wrote this shit&quot;.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
And then, the next morning when you wake up, you do a git log and you see this message pop up on your screen and jump out of your seat. Well, if you have not pushed your changes upstream, i.e. the commit still resides only on your local repository, it would really serve a noble purpose if you actually make the message more sensible. Here&#39;s how you can do that.&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;git commit --amend -m &quot;This commit has all the equations that prove the String Theory&quot;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The above command will simply edit the commit message of the latest commit.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
However, if you have done something more grave, like missed out on files that were supposed to be added in the previous commit, then you can simply add them using the git add . command then use the git commit with the --amend option in the same way as shown above. Basically what you did was that you rebuilt the tree that represents the files of this commit, discarded the previous commit and created a new one in its place. You can say so because when you amend a commit, it results in a new commit checksum.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Hope this helps!&lt;/div&gt;
&lt;br /&gt;
Signing Off&lt;br /&gt;
Ryan&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/git-editing-previous-commit.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-816359651437047157</guid><pubDate>Wed, 19 Dec 2012 16:09:00 +0000</pubDate><atom:updated>2012-12-19T21:39:36.601+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>Timer Chaining in JavaScript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Only recently, I was working on a project where I had to implement a feature that is usually implemented using gif. What I had to do was to cause 4 images - &amp;nbsp;lets say 1, A, B, C overlayed on each other to display sequentially. And then, after the first set of 4 images have been displayed, have a similar sequence repeat, but with only the first image changed, i.e. 2, A, B, C. And then the third set - 3, A, B, C.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now, since the sample that I had to replicate was made in gif, I presume that creating such an image using an image editing software is pretty easy. My task was to replicate this effect using pure javascript. Moreover, since it was only in the development phase, I had to design for flexibility, since requirements can change at any moment of time. Thankfully, for me, designing for flexibility is a huge turn on. Albeit inititally difficulty, the immense amout of satisfaction that you gain out of it when you see the requirements change and be able to tell your clients that you can implement the changes in like 10 seconds is totally worth the time and energy that you put in during the initial development phases.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
For my experiment, the flexibility that I had to incorporate was to be able to change the duration for which each of the 4 images can be displayed if the requirements changed. Had it been a gif file, it wold have been an easy task, just change the time in the application and then it would be ready. Show the client, and if its not what he/she wants, then redo the same.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I wanted to achieve something similar in JavasScript. However instead of a tool I wanted to create a system wherein you can programmatically configure the time duration for each image and then automatically have the effect ripple throughout all the sequences of images.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Also, instead of restricting the system to that of 4 images, I wanted it to be extensible such that it would be able to incorporate any number of images that could be overlaid on top of each other. Also, I wanted the system to behave in a way that incorporates a slightly different functionality - not be restricted to images being overlaid.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So, ultimately, after thinking about all the flexibility that I wanted to incorporate into the system, it turned out that what I wanted to create was a system of timers such that each timer could be chained to each other yet they would be oblivious to characteristics of the next timer. Also, these timers would have a feature that would let them bind the timer ending function for the next timer without knowing what the function does. And the beauty would be that each of these functions can be chained to an infinite level.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So, how did i proceed to make such a system.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
First things first.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I designed the system as a phase based system. Since I had 4 images in my sequence, I created a object that saved the properties of 4 phases and references to the functions that would be invoked on the terimination of each function. I called this object a phasedTimeout object, which is essentially nothing but a nested object.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The first thing that I did was - created an object that contains all the values for which i want each of the phases to execute.&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var settings = {
  duration1:2000, //Millisecond duration for the first timer
  duration2:300, //Millisecond duration for the second timer.
  duration3:300, //Millisecond duration for the third timer.
  duration4:300, //Millisecond duration for the fourth timer.
 };
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the next part, I define the different phases, and the functions that can be executed at the end of each phase.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var phaseTimeouts =  {
  &#39;phase1&#39;:
   {
    duration:settings.duration1,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase2&#39;:
   {
    duration:settings.duration2,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase3&#39;:
   {
    duration:settings.duration3,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase4&#39;:
   {
    duration:settings.duration4,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
 }
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
And then, finally on window loading, ( because it was part of my requirement to do all of this only after the images on the page have loaded), i created a function that chains together the different phases.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var timeOutSetter = function(){
  
   //Call the terminating function of the previous phase
   phaseTimeouts[&#39;phase&#39;+currentPhaseId].end(currentPhaseId);
   
   //If there is anything left in phaseTimeouts array
   //create a new timer and update the new timerId
   var nextPhaseKey = &#39;phase&#39;+(currentPhaseId+1);
   if(phaseTimeouts[nextPhaseKey]){
    currentPhaseId++;
    console.log(&#39;Entering Next Phase : &#39; + nextPhaseKey);
    currentTimerId = setTimeout(timeOutSetter,phaseTimeouts[nextPhaseKey].duration);
   }
  };
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The above function does nothing but chains the different timers together by using the settings from the phaseTimeouts object. The variable phaseTimeouts is a misnomer since it captures more than just the timeout, and can perhaps be used to bind extra functionality.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
For my experiment, I toggled the visibility of different images inside the &#39;end&#39; function of each of the phases. But the fact that &amp;nbsp;you can have any number of phases and that each phase has a different function that handles the phase end that is independent of each other makes it all the more useful.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Below is the piece of code in its&amp;nbsp;&lt;span style=&quot;text-align: left;&quot;&gt;entirety&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:js&quot;&gt;$(function(){

 var settings = {
  duration1:2000, //Millisecond duration for the first timer
  duration2:300, //Millisecond duration for the second timer.
  duration3:300, //Millisecond duration for the third timer.
  duration4:300, //Millisecond duration for the fourth timer.
 };
 
 var phaseTimeouts =  {
  &#39;phase1&#39;:
   {
    duration:settings.duration1,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase2&#39;:
   {
    duration:settings.duration2,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase3&#39;:
   {
    duration:settings.duration3,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
  ,
  &#39;phase4&#39;:
   {
    duration:settings.duration4,
    end : function(currentPhaseId){
     console.log(&#39;Ending phase : &#39;+ currentPhaseId);
    }
   }
 }
 
 //Once all the contents of the window finishes loading, start the slideshow
 $(window).load(function(){
  
  var currentPhaseId = 1;
  var currentTimerId = 0;
  
  var timeOutSetter = function(){
  
   //Call the terminating function of the previous phase
   phaseTimeouts[&#39;phase&#39;+currentPhaseId].end(currentPhaseId);
   
   //If there is anything left in phaseTimeouts array
   //create a new timer and update the new timerId
   var nextPhaseKey = &#39;phase&#39;+(currentPhaseId+1);
   if(phaseTimeouts[nextPhaseKey]){
    currentPhaseId++;
    console.log(&#39;Entering Next Phase : &#39; + nextPhaseKey);
    currentTimerId = setTimeout(timeOutSetter,phaseTimeouts[nextPhaseKey].duration);
   }
  };
  
  var currentTimerId = setTimeout(timeOutSetter,phaseTimeouts[&#39;phase1&#39;].duration);
  
 });
 
});

&lt;/pre&gt;
&lt;br /&gt;
Decoupled designs can be so addictive, is&#39;int it?&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Signing Off&lt;/div&gt;
&lt;div&gt;
Ryan&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/timer-chaining-in-javascript.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-3735356673733310191</guid><pubDate>Wed, 12 Dec 2012 19:43:00 +0000</pubDate><atom:updated>2012-12-13T01:16:24.536+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Git : Status Of Working And Staged Files</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;I just came to know about this very neat feature of the git status command.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Basically, the status command gives you the the names of the files which you are tracking but have certain changes in them relative to your last commit.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now, Git has 3 places in which it keeps the code - the commit area, the stage area/index and the working directory.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Lets take a few scenarios to explain the usefulness of this command&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;span style=&quot;color: #38761d; font-family: Georgia, Times New Roman, serif;&quot;&gt;Scenario 1&lt;/span&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Lets say that you have a file called Sample_File.txt in the working directory to which you made a few changes. But you have not staged it yet. So, the state of the file in the three repositories can be described as &amp;nbsp;(&lt;i&gt;latest commit == staged) &amp;nbsp;!= (working).&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;To know which files have been modified in the working directory, in a very shorthand form, use git status with the -s option. Here&#39;s what I got when I ran it on my system.&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_qF3pYNjqYSH6Jmdmac6Q5elGnTNKFgGpYiyuHWEMLdBnHm3gr9_hlyJZ8LLgUrJW4gwibejxcRaIp9fpGIFA8Yqlx4ydrPLkrDL7IBH_SNsfQi0xLGDQRNIIXwO4r2zi6k3pUvL8Ytg/s1600/scenario1.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_qF3pYNjqYSH6Jmdmac6Q5elGnTNKFgGpYiyuHWEMLdBnHm3gr9_hlyJZ8LLgUrJW4gwibejxcRaIp9fpGIFA8Yqlx4ydrPLkrDL7IBH_SNsfQi0xLGDQRNIIXwO4r2zi6k3pUvL8Ytg/s1600/scenario1.png&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Notice that the M is in Red, and there is a little bit space to its left, which means, that the file is modified in your working directory. (The extra space is explained in scenario 2)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The status code M stands for &#39;Modified&#39;. There are many other one letter status codes that you can take a peek at &lt;a href=&quot;http://www.kernel.org/pub/software/scm/git/docs/git-status.html&quot; target=&quot;_blank&quot;&gt;over here&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;span style=&quot;color: #38761d; font-family: Georgia, Times New Roman, serif;&quot;&gt;Scenario 2&lt;/span&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Lets say that you have a file in your working directory, that has some changes but you staged it after making these changes.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In this case&amp;nbsp;the state of the file in the three repositories can be described as&amp;nbsp;&amp;nbsp;(&lt;i&gt;latest commit)! = (staged &amp;nbsp;== working )&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now if you run git status -s, this is what you get&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOmQiaCGew0RCz6lsNGe_3OS2LDkGuWvmWOb93EI5c_8KI66Iez2GVQCklOJ8XNLMCW7GigCQubJFewPc8T_P7gIU8roJyWORYauHaMdxyFdGppcVV5OtdtBhIADYlI9OfKIB2Dyr44Tk/s1600/scenario2.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOmQiaCGew0RCz6lsNGe_3OS2LDkGuWvmWOb93EI5c_8KI66Iez2GVQCklOJ8XNLMCW7GigCQubJFewPc8T_P7gIU8roJyWORYauHaMdxyFdGppcVV5OtdtBhIADYlI9OfKIB2Dyr44Tk/s1600/scenario2.png&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Notice that the M is in Green, but this time there no space to its left, which means, that the file is in a modified state in your staged directory. You can however notice that there is some extra space between the M and the file name this time. (Will be clarified in Scenario 3)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;span style=&quot;color: #38761d; font-family: Georgia, Times New Roman, serif;&quot;&gt;Scenario 3&lt;/span&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Lets say that your made changes to your file, staged it and then edited it again.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In this case&amp;nbsp;the state of the file in the three repositories can be described as &amp;nbsp;(&lt;i&gt;latest commit ! = staged &amp;nbsp;!= working )&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRsFXvWoFhJtYGRl-SZlfnAVN2savuXZv0XshuUv4vgh6zRJi84hQAnoxKeaIzgl1nvFpRwDEvzZifC8PNQWlIlDh0V6fbehc9Cu4oaaTXUXaQyhTUkVVWeDbrfrTdowf7j_OIviOtZLQ/s1600/scenario3.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRsFXvWoFhJtYGRl-SZlfnAVN2savuXZv0XshuUv4vgh6zRJi84hQAnoxKeaIzgl1nvFpRwDEvzZifC8PNQWlIlDh0V6fbehc9Cu4oaaTXUXaQyhTUkVVWeDbrfrTdowf7j_OIviOtZLQ/s1600/scenario3.png&quot; /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;i&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Now you can see clearly that you have 2 M&#39;s. One in red indicating the state of your file in the staged area and one in green indicating the state of your file in the working directory.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Its quite common that throughout your workflow, you would find yourself moving between the above scenarios sequentially. (Scenario 1 -&amp;gt; Scenario 2 -&amp;gt; Scenario 3) . The above set of commands should be able to help you keep track of what files were changed where.&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; text-align: justify;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; text-align: justify;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;References&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;a href=&quot;http://www.kernel.org/pub/software/scm/git/docs/git-status.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The git manual reference&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Signing Off&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Ryan&lt;/span&gt;&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/git-status-of-working-and-staged-files.html</link><author>noreply@blogger.com (Anonymous)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_qF3pYNjqYSH6Jmdmac6Q5elGnTNKFgGpYiyuHWEMLdBnHm3gr9_hlyJZ8LLgUrJW4gwibejxcRaIp9fpGIFA8Yqlx4ydrPLkrDL7IBH_SNsfQi0xLGDQRNIIXwO4r2zi6k3pUvL8Ytg/s72-c/scenario1.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-1814730359414046021</guid><pubDate>Tue, 11 Dec 2012 19:31:00 +0000</pubDate><atom:updated>2012-12-12T01:02:08.796+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Using Google Drive As A Web Host</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Okey, another one of my experiments. I came to know a few days ago that Google &lt;a href=&quot;http://googleappsdeveloper.blogspot.com/2012/11/announcing-google-drive-site-publishing.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;officially announced&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/a&gt; how you can now use your Google Drive account as a web host.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;So, I decided to make use of this awesome new feature and tried to create and host my profile on Google Drive itself. In this post, I am simply going to tell you what you need to do to get up and running with Google Drive as your web host in its simplest form.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Things that you need to do to setup Google Drive as your web host&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;Go to Google Drive&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
Of course, you gotta have a Google account to begin in the first place and then just type in &lt;a href=&quot;http://drive.google.com/&quot;&gt;drive.google.com&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;Create a public folder&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
This is important. If you want to use Google Drive as a web host, you need to keep your files in a public directory. Create a new folder and then simply choose the &#39;share&#39; button and give it public access. Lets assume my folder is called &#39;ryan&#39;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;Dumping your static files&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets say that your site has a lot of html pages, css stylesheets, jQuery, javascript, images - the usual stuff. Just add all your files in the same directory structure in which they are present in your local system to the public folder that you just created. So, all my files are dumped into the public folder called &#39;ryan&#39; in my google drive.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;Naming convention&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
Ensure that at least one file in the public folder is named as index.html. This will act as the landing page for your site.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;What to publish&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
Now comes an important step. In order to share your site, you need to share a link to the public folder. Initially I thought that you would need to share the link to the index.html but that did not work as expected.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So what you need to do is, &amp;nbsp;note the google drive file-id of your public folder. To do that, just open your public folder in google drive. By default it lists all the files in that folder. In the url at the top, you will see that it contains a long string of garbled aphanumeric characters after the last slash. That is your folder id. Copy it.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;Creating a url to publish&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;
Now all you need is a url that points to your folder. To create your url, simply append the file id to the following string - http://googledrive.com/host/&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
For eg. The raw link to my profile on google drive is &lt;a href=&quot;http://googledrive.com/host/0B87xZT3F0rDcZlVONVRVY3QtZFE&quot;&gt;http://googledrive.com/host/0B87xZT3F0rDcZlVONVRVY3QtZFE&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;And that pretty much does it. Clicking on the above URL serves the index.html page of your public folder.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Other Tips&lt;/span&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;If you want to use a neat little name instead of the long string of garbled alphanumeric characters that appear at the end of the url, go to &lt;a href=&quot;http://gdriv.es/&quot;&gt;Gdriv.es&lt;/a&gt; and convert your folder id custom name. That&#39;s what I did for my profile, and now it looks like this -&amp;nbsp;&lt;a href=&quot;http://gdriv.es/ryansukale&quot;&gt;http://gdriv.es/ryansukale&lt;/a&gt;. Much more neat i&#39;d say.&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Sometimes its just easier to download Google Drive for the desktop, edit your files right in your local machine and then watch them automatically get uploaded.&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;When multiple pages are interlinked, I have observed that the url in the address bar remains the same. So, I guess bookmarking will be an issue in this case.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Signing Off&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Ryan&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/google-drive-as-web-host.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-7423635121525895868</guid><pubDate>Mon, 10 Dec 2012 16:02:00 +0000</pubDate><atom:updated>2012-12-10T21:36:17.101+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>Handling Page Unload Using JavaScript</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Recently for one of my projects, I was working on trying to implement some functionality wherein if the user tried to leave web page by any other means other than submitting a form, (s)he would be prompted for confirmation. It was part of one of the research experiment surveys that I was designing. So, if the user left the page, we would simply discard the user&#39;s responses as invalid because partial responses are of no use to do any meaningful analysis.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Apparently, there are plenty of ways in which a user can close a webpage - the back button, the close button, or maybe some other gestures that I might be unaware of that are present in some of the newer web-browsing applications/devices.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;What I found interesting was that there is a very simple and convenient way to check when a page is being &#39;unloaded&#39; from a browser window. Javascript lets you bind a neat little handler to the &#39;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;window.onbeforeunload&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&#39; property which tells the runtime that a web page is going to be unloaded.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Being a jQuery fanatic, I usually tend to use jQuery for all such stuff, since it makes maintenance a far easier task than writing pure JavaScript. So, my jQuery instincts told me to use the new &lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;a href=&quot;http://api.jquery.com/on/&quot; target=&quot;_blank&quot;&gt;on&lt;/a&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt; method to bind an event handler to the event. And it did work pretty good. All I did was this -&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: js;&quot;&gt;window.onbeforeunload = function(){ return &quot;Are you sure 
you want to leave the page?&quot;; };
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Oh, I am sure that message looks familiar on many marketing sites. You can be assured that I am not creating one more such website. In fact, even the message that I displayed was &amp;nbsp;something completely different. This is just meant to be a sample.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Easy peasy, and that works. Now, the issue comes when the user actually submits the form. The issue is that this function fires if the user leaves the page in any way, which does make a lot of sense, because after all, the user is leaving the page.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;However, what I did not want was for the function to fire when the user submitted the form. So, what did I do? I just created a submit handler for my form and then tried to unbind the event handler using the &lt;b&gt;&lt;i&gt;&lt;u&gt;&lt;a href=&quot;http://api.jquery.com/off/&quot; target=&quot;_blank&quot;&gt;off&lt;/a&gt;&lt;/u&gt;&lt;/i&gt;&lt;/b&gt; function of jQuery.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Neat as it may sound, that did not work. I am still not sure why. However after going through a couple of posts wondering what I had done wrong, it dawned on me that a better way to handle this feature was to both bind and unbind an event handler for this function directly using pure JavaScript.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;So, this is what i eventually ended up doing&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush: js;&quot;&gt; //For binding
 window.onbeforeunload = confirmExit;
 function confirmExit()
 {
  return &quot;Are you sure you want to leave this page?&quot;;
 }
 
 //For unbinding
 $(&#39;#myForm&#39;).submit(function(event){
  window.onbeforeunload = null;
 });

&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;And that&#39;s pretty much all it took to create a page that warns the user before (s)he accidentally (or intentionally) attempts to leave a web page.&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Also, after completing my piece of code, i stumbled across this &lt;a href=&quot;http://api.jquery.com/unload/&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;i&gt;jQuery unload&lt;/i&gt;&lt;/b&gt;&lt;/a&gt; function, that I beieve pretty much does the same thing.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Signing Off&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Ryan&lt;/span&gt;&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/page-unload-javascript-event.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-8809419902197274285</guid><pubDate>Mon, 10 Dec 2012 15:24:00 +0000</pubDate><atom:updated>2012-12-10T21:34:06.838+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Change Image Resolution in Powerpoint</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Recently for one of my projects a teammate had created a simple Powerpoint presentation , the slides of which we later decided to use as a mockup for a website.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;We wanted to make a video using these slides emulating the user interaction with the pages. I believe that the video authoring tool that was being used required images in order to create a video. Now, I haven&#39;t worked on any video authoring tool before, but then, of course I am supposed to be knowing a lot more about tech than I actually do now.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;So, I said that I would try to export the slides as images and that they could be used by my teammate who was making the video.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;I was making use of Microsoft Powerpoint 2010, which is a very good piece of software. Hopefully many of you would agree if you are using it. Well, if you don&#39;t agree, I am never in a mood to argue about Microsoft software.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;One neat feature that Powerpoint has is that it lets you save your slides in the form of jpeg images. Just go to File-&amp;gt;Save as-&amp;gt;, and select the file type to be jpeg images.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;The interesting thing is that the images exported are not of such a high quality. So, when my teammate added them into the video, most of the text got blurred.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Well, there is a crazy way to increase the resolution of the images when you are saving a Powerpoint presentation as a set of images. I could &amp;nbsp;not find any options from the UI, but &lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;i&gt;it can be done by adding a registry entry&lt;/i&gt;&lt;/b&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Simply type in regedit in the command prompt or in the run manager or in your start menu.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Expand the registry to&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\PowerPoint\Options&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Add a new registry key from Edit-&amp;gt;New DWORD Value&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Name the new key&amp;nbsp;ExportBitmapResolution and set its (Decimal) value to anything among these - 50, 96, 100, 150, 200, 250, 300. Maybe you can try other values and see if tha works out well for you.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif;&quot;&gt;Open Powerpoint again, and then try to save your file as a set of jpeg images. This time your pictures will be of a different resolution depending upon what decimal value you provided for the new registry key.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;I am not registry expert but if you have some other version of Microsoft Windows or having some other issues, check out the official documentation at their site &lt;a href=&quot;http://support.microsoft.com/kb/827745&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&amp;nbsp;and figure them out.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Signing Off&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Ryan&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/12/change-image-resolution-powerpoint.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-7839963139113603947</guid><pubDate>Thu, 29 Nov 2012 21:12:00 +0000</pubDate><atom:updated>2012-11-30T02:44:56.021+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">GITHUB</category><title>Github : File Changes and Commit History</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Most of the times, when working with Github, you will want to see the differences between the current local copy of a file and the previous commits. One of the easiest ways to see it visually would be to make use of the gitk command and give the file name as the argument. e.g.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;gitk &amp;nbsp;filename&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thats all. This command would open up a neat little window and show you all the changes that were made to this file across different commit and lets you compare it with the version in your current working directory. Oh, and do remember to include the relative path before the filename. Standard convention.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Signing Off&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Ryan&lt;/div&gt;
&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/11/github-file-changes-and-commit-history.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-2604582544384957033</guid><pubDate>Tue, 23 Oct 2012 19:35:00 +0000</pubDate><atom:updated>2012-10-24T01:05:56.211+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Productivity</category><title>Keyboard Shortcuts For Gmail</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
The more you work, the less you want to move your hands away from the keyboard. And most of us spend shitload of time browsing through emails. The last thing that you want to do is have to use your mouse when you want to do bulk actions using email such as deleting, archiving or moving emails to specific labels.&lt;br /&gt;
&lt;br /&gt;
If by any chance you are gmail user, you have a lot of options to improve your productivity - at least during email time.&lt;br /&gt;
&lt;br /&gt;
So, lets cut the crap and get to the point.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Things to get you started with using keyboard shortcuts in gmail&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Setup your gmail to make use of keyboard shortcuts.&lt;/li&gt;
&lt;li&gt;Know where to look for help when you start learning or when you forget a shortcut.&lt;/li&gt;
&lt;li&gt;Memorize the common shortcuts that are relevant to YOU.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;1) Setup your gmail to make use of keyboard shortcuts&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Go to the cog icon in the new gmail interface (new as of this writing in Oct 2012)&lt;/li&gt;
&lt;li&gt;Go to &#39;Settings&#39;&lt;/li&gt;
&lt;li&gt;Look for the keyboard shortcuts option and enable it.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;
&lt;b&gt;&lt;i&gt;2) Know where to look for help when you start learning or when you forget a shortcut.&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
There are 2 ways you can do this&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Instant help : Press &lt;b&gt;?&lt;/b&gt; anywhere in your gmail (while you not typing, of course). You get a list of available shorcuts.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://support.google.com/mail/bin/answer.py?hl=en&amp;amp;answer=6594&quot; target=&quot;_blank&quot;&gt;Official google documentation&lt;/a&gt; - List of all the shortcuts with their explanation&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;3) Frequenty Used ones you might &lt;span style=&quot;color: red;&quot;&gt;LOVE &lt;/span&gt;to have at your finger tips&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Select conversation - &lt;b&gt;x&lt;/b&gt; (One of the most basic ones, You definitely would want to learn this one to &amp;nbsp;combine it with others)&lt;/li&gt;
&lt;li&gt;Next conversation - &lt;b&gt;j&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Prev conversation - &lt;b&gt;k&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Delete conversation - &lt;b&gt;#&lt;/b&gt; (This is just so important!)&lt;/li&gt;
&lt;li&gt;Move to label - First select using x , and then press v (Definitely learn this one)&lt;/li&gt;
&lt;li&gt;Go to inbox - &lt;b&gt;g&lt;/b&gt; then &lt;b&gt;i&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Compose - &lt;b&gt;c&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Search - &lt;b&gt;/&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Reply - &lt;b&gt;r&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Reply All - &lt;b&gt;Shift + a&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Mark as read - &lt;b&gt;Shift + i&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Mark as unread - &lt;b&gt;Shift + u&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Move to chat search - &lt;b&gt;q&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/10/keyboard-shortcuts-for-gmail.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-3739040542209167319</guid><pubDate>Wed, 04 Jul 2012 16:48:00 +0000</pubDate><atom:updated>2013-04-23T21:02:21.555+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">TOOLS</category><title>Useful Google Chrome Extensions</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Over a period of time, I came across a couple of good Google chrome extensions that have helped me quite a bit in getting things done.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This post is nothing but just a short list of some really useful google chrome extensions that you might find useful if you are a developer or if you spend most of your time online working on Google Chrome and like to organize your stuff right from your browser window.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As a good practice, I mostly keep the extensions disabled and only enable the ones I need when i need them such as Awesome Screenshot or the Alarm clock. If you keep too many of these active, Chrome takes extra time to startup, which is something that none of us want.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Sometimes its just better to use different extensions in different chrome profiles and you can use each profile for a different purpose.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Here&#39;s the stuff that I use. Click on the titles to go to the extension in the chrome extension gallery. If you are aware of better alternatives to some of these, I would love to try them out, so don&#39;t hesitate to share the goodies!&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/njhgeimnepehieioinbhmfpphfoocmng&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Springpad Extension&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Springpad is a free application that makes it quick and easy to take notes and save anything you want to remember - from tasks and lists to products, places, movies, recipes and more. Everything you save is automatically organized and enhanced with useful information for instant access on the web and on your phone&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;http://www.chromeextensions.org/utilities/chrome-youtube-downloader/&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Chrome YouTube Downloader&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Download youtube videos in different formats. Such extensions keep on failing whenever there is a new update to youtube, so I am not sure if this one is still working at the time that you are reading this post. Remember to check the user comments before installing this extension.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/alelhddbbhepgpmgidjdcjakblofbmce&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Awesome Screenshot: Capture &amp;amp; Annotate&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Capture the whole page or any portion, annotate it with rectangles, circles, arrows, lines and text, blur sensitive info, one-click…&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/acgdjjilmhiofacmdnmmlndeokamkkcl&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Instapaper&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Add the current page to your Instapaper List&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Similar to pocket. A single click on the icon will save the current page to your Instapaper unread list. A double click will take you to the Instapaper unread list.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/obhefmbclkekanpjjpkbciloojcmpkap&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Todo.ly&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Todo.ly is an intuitive and easy to use online Todo list, and Task Manager. It helps you to get organized, and to get things done.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/dplbpgapoedppajbikieafefmcceaagn&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Easy Clock&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Easy to use world clock. Find time for global meetings easily. Do time zone conversions among cities intuitively.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/npmllfhdnjcijofddghkhhknagamimip&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Facebook Lite for Chrome&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Receive notifications and quickly access everything you love about Facebook with Facebook Lite for Chrome.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/jgoepmocgafhnchmokaimcmlojpnlkhp&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Google +1 Button&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
+1 and share a web page, anywhere you go on the web.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/gmbgaklkmjakoegficnlkhebmhkjfich&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Google Calendar&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Easily check Google Calendar and add new events from websites you visit.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/mgijmajocgfcbeboacabfgobmjgjcoja&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Google Dictionary (by Google)&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;View definitions easily as you browse the web.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
With this extension, you can:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Double-click any word to view its definition in a small pop-up bubble.&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;View the complete definition of any word or phrase using the toolbar dictionary.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/kfhbeoladmfbfpmdobbakmokhlniaodg&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;OmniMail for GMail&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Quickly send email from the address bar using GMail™!&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
OmniMail helps you send emails faster! Easily start new emails from the address bar/omnibox.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/niloccemoadcdkdjlinkgdfekeahmflj&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Pocket (formerly Read It Later)&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Pocket Extension for Google Chrome – The best way to save articles, videos and more&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you find something on the web that you want to view later, put it in Pocket. It automatically syncs to your phone, tablet or computer so you can view it at any time, even without an internet connection.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/cppogeekogbladboceekjeiibihnkbhp&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Send to Google Docs&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Allows you to upload a pdf version of the current page to your Google Docs using the pdfcrowd service.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This extension&#39;s purpose is to make it easy to save a web page to your Google Docs.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/happachemjeninagaojbimmpkmmeajlg&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Talking Alarm Clock&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;A clock with two configurable alarms that will play a sound and speak a phrase of your choice.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This extension gives you a handy and fun alarm clock that&#39;s at your fingertips whenever you&#39;re running Chrome!&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Two separate alarms, with handy controls to adjust by 5-minute intervals - or type any exact time with 1-minute precision.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/kpifmiaadiihnkolggaepacodfmgceki&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Windows Live Messenger Extension&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Runs Windows Live Messenger in Google Chrome.Connect with friends and family with Windows Live Messenger (MSN Messenger). Sign in with Windows Live ID and chat with your friends. This one has a good sound alert&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/jdnejaepfmacfdmhkplckpfdcjgbeode&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Yet Another Extension for Google Bookmark&lt;/span&gt;s&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
YAGBE - Displays Google Bookmarks in a tree. Supports nested labels, searching, sorting, creating, and managing Google Bookmarks.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr style=&quot;text-align: justify;&quot; /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://chrome.google.com/webstore/detail/mmfklpmdfldnnjbkdmamhokiphfkfieg&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Sticky Notes&lt;/span&gt;&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Quick and easy notes for Chrome : Sticky Notes is a Chrome app that locally stores all your notes in a single tab. It auto saves on every letter, so you don&#39;t have to worry about losing something because your computer crashed and you didn&#39;t click Save - it&#39;s already done for you!&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;a href=&quot;https://chrome.google.com/webstore/detail/oeopbcgkkoapgobdbedcemjljbihmemj&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Checker Plus for Gmail&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
Displays new emails and the sender&#39;s contact photo, get desktop notifications or even listen, read or delete them without opening a tab.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;
</description><link>http://mycodefixes.blogspot.com/2012/07/useful-google-chrome-extensions.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-4085251933458581772</guid><pubDate>Mon, 25 Jun 2012 10:54:00 +0000</pubDate><atom:updated>2012-06-25T16:24:40.214+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>jQuery makeArray And Array Lengths</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Today I just came to know about a pretty cool thing when i was going through the documentation of the &lt;a href=&quot;http://api.jquery.com/jQuery.map/&quot; target=&quot;_blank&quot;&gt;jQuery.map&lt;/a&gt; function. Now, the jQuery makeArray function is pretty useful because it can convert objects into arrays. But there is one restriction, your objects need to be array like objects. And what are array like objects? Array like objects are objects that have a length property. And I think that it would be safe to presume that he most common array like object known to mankind is the arguments object that is available in javascript functions.
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Well, arrays in JavaScript are pretty cool because you can just remove elements from the end of an array by doing a very simple thing. Modifying the length property of the array. For example, lets say that you have an array 

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var myArray = [1,2,3,4,0,0,0];

&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now you want to keep only the first four elements. What do you do? You simply change the lenght of the array to 4. And voila you&#39;re done. 
While that&#39;s cool, I was hoping that if you want to delete elements from the beginning of the array, you could set the length to a negative value. But sadly, that does not work :(. Well, you could always resolve to the good ol technique of using the shift() function. Or you could choose to reverse() and then pop(). Depends on your requirement.

&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
But the interesting thing was that when using the makeArray function, if the length property is not set to a proper value, you will end up with an incorrect array. Taking this example right from the jQuery documentation page for the map function

&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;// The following object masquerades as an array.
var fakeArray = {&quot;length&quot;: 1, 0: &quot;Addy&quot;, 1: &quot;Subtracty&quot;};

// Therefore, convert it to a real array
var realArray = $.makeArray( fakeArray )

// Now it can be used reliably with $.map()
$.map( realArray, function(val, i) {
  // do something 
});&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see the fakeArray has a lenght property set to 1. When you invoke makeArray, you get an array containing only 1 element - [Addy]. What just happened to the second element??? It seems that the makeAray function converts the object into an array only for &#39;length&#39; number of elements. Hence, when using this function, just beware of this behaviour and make sure that the lenght property is equal to the number of properties in your object (not counting the lenght property itself), or you might just be in for a little surprise!&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
Signing Off&lt;br /&gt;
Ryan Sukale&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/06/jquery-makearray-and-array-lengths.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-7830463903879110880</guid><pubDate>Thu, 07 Jun 2012 07:55:00 +0000</pubDate><atom:updated>2012-06-07T13:25:28.620+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked Javascript - Prototypes and Inheritance</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In part 9 of this series &lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Naked Javascript&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;, I am going to talk discuss inheritance in Javascript. In order to understand the concept of inheritance, it definitely hepls to understand the concept behind functions and closures in Javascript. You can check out the previous article on closures and this article on functions to brush up on the basics of these concepts.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Inheritance in Javascript is a bit different from inheritance in other programming languages like Java. The difference lies not only in terms of the syntax but even the implementation is so staggeringly different that it may sometimes not be an easy task to grasp without knowing a few other concepts in javascript.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets start from the root of the problem.&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Functions&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As I have mentioned multiple times throughout the series, functions are first class objects in Javascript. The underlying reason is simple. In Javascript, everything is an instance of the class Object. This is somethign that is taken from Java. The Object class provides a default implementation of several commonly used functions, for example the &#39;toString&#39; function. Now, to think about it, since all javascript objects need this functionality, they must somehow inherit this function from the object class.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
There are 2 important things that you need to remember when dealing with inheritance in JavaScript.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
1) Since JavaScript does not have classes, inheritance in JavaScript is achieved by The
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
2) The mechanism by which two objects are connected in an inheritance hierarch is via the &#39;prototype&#39; property of objects. 
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets talk about &#39;prototype&#39;.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you create any Function in javascript, javascript implicitly defines a new property on that object called &#39;prototype&#39;. The prototype property is no ordinary property.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets see it with a simple example
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;//Here I have defined a simple constructor function
function Employee(name){
 this.name=name;
}

//Set the default age
Employee.prototype.age = 16;

var emp1 = new Employee(&#39;xyz&#39;);
console.log(emp1.age);
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is the simplest example that demonstrates the use of the &lt;b&gt;&lt;i&gt;prototype property&lt;/i&gt;&lt;/b&gt;. 

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the first few lines, I defined an ordiary function constructor that would let me create Employee objects. We know that functions are objects. And as I stated earlier, every object gets its own prototype property. Therefore we were able to define a variable called &#39;age&#39; on the prototype property of the employee class and set its value to 16.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the last few lines, notice that although we did not declare any property called &#39;age&#39; on the Employee instance inside the Employee constructor function, we did not get an &#39;undefined&#39; when we printed the value of the &#39;age&#39; property on the console. The reason this happened is because we defined a property called &#39;age&#39; on the prototype property of the Employee function. 

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
What happend here is that when an attempt was made to access a property that is not present on the &#39;empl&#39; object, the prototype of the constructor function was searched for the property of the same name. So in this case, when we accessed emp1.age, since the JavaScript engine was unable to find the property on the object itself, it tried to lookup a property called &#39;age&#39; on the Employee.prototype object. This leads us to the conclusion that prototypes are a good place to declare defaults.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now lets see what happens when we attempt to modify a property on an object that was defined only on the prototype.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name){
 this.name=name;
}

//Set the default age
Employee.prototype.age = 16;

var emp1 = new Employee(&#39;xyz&#39;);
var emp2 = new Employee(&#39;abc&#39;);
console.log(emp1.age);
emp1.age++;
console.log(emp2.age);
console.log(emp1.age);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you check the console, you will notice that once you modify the property on the object, that was initially defined only on the prototype, the object will then begin to own a local copy of the property. In our case, when we wrote emp1.age++, this is what we would normally expect the program to do : -
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;emp1.age = emp1.age + 1;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
But, this is what the js engine executed
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;emp1.age = Employee.prototype.age + 1;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thats because, before adding doing the addition, emp1.age does not exist. And thats the reason why, on the RHS of the equals operator, emp1.age becomes Employee.prototype.age
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The direct implication of this feature is that the prototype property of Functions becomes the most suitable candidate for declaring constants and other properties that will not be modified by instances created by that funcion but which are required for every instace of the class.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name){
 this.name=name;
}

Employee.prototype.getName=function(){
 return this.name;
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This solved the problem of creating a single place to define functions that can be reused by all instances.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
However, what if you want a function or a property to be available for all functions? 
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The answer is pretty simple. Define the property/function on the Function.prototype. For example.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;Function.prototype.PI=3.14;

var dummyFunction = function (){};

console.log(dummyFunction.PI);

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Note the syntax that we used to access the property PI. We directly made use of the function name. Reiterating myself, this is possible because every function is an object in itself whose constructor is the Function method.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Inheritance&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Inheritance is a little bit twisted in Javascript as compared to Java and other similar languages. Thats because in Javascript, objects inherit from objects. The prototype property plays a key role in implementing inheritance. However, it can also be a bit misleading at times. Consider the following example.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this example, we want to create a Class whose objects inherit certain protperties from another Object. The simplest example to represent this form of inheritance would be
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;//The doom object is the parent object
//You want to inherit its functions and properties in all
//instances of the MyClass class
var doom = {name:&#39;doom&#39;,
   baseMethod:function(){
    console.log(&#39;defined on doom&#39;);}
   };

//Your class whose instances you want to create
function MyClass(){
 //Some properties
}

//Set the prototype property to the &#39;doom&#39; object
MyClass.prototype=doom;

//The following invocation wont work because the function is defined on
//the prototype of MyClass. Therefore, MyClass itself
//cannot access the variables of doom directly without
//the prototype property
//MyClass.baseMethod();

//This works because instances of the class
//can access the variables defined in the
//prototype directly.
var mc = new MyClass();
mc.baseMethod();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The secret to the functionality of the prototype is intuitivley simple. 
Every object in javascript has a hidden property called __proto__. Observe the 2 underscores before and after the word proto. When you create an object in javascript, even if it is an empty object, the value of the &#39;__proto__&#39; property is set to a default object - the Object object, that contains a number of useful methods like toString. Type the following lines in your browser console to verify this
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var obj = {};
console.dir(obj.__proto__);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You find that the __proto__ references a default object with a set of functions.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Following our previous example, type the following in your browser console
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;console.dir(mc.__proto__);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You will find that it points to the doom object. The underlying principle is very simple. When you created an object of type MyClass, something similar to this would have executed under the hood.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;mc.__proto__=Myclass.prototype;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
And since you have already set MyClass.prototype to reference the doom object, you are all set to inherit the properties of the doom object.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So, whenever you create an object, imagine an invisible link from the &#39;__proto__&#39; of the object to the &#39;prototype&#39; property of its constructor. For ordinary objects the constructor is the Object class and for your own custom classes, the constructor is the constructor function of your classes. You might want to take a look at&amp;nbsp;&lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/2012/05/constructing-objects-javascript.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;this article&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&amp;nbsp;to learn more about using functions as constructors.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Another implication of this feature is that, if at any point of time, you want that all objects of your class should inherit a different set of properties, you can simply change the &#39;prototype&#39; variable in the function name to a different object. That would be equivalent to dynamically swapping the a parent class at runtime, thereby enriching all the existing objects with a new set of functions, which is something is something absolutely cool because thats not something that you can do in languages like Java. What&#39;s more is that if you want to change the parent class of only one single object, you can change the &#39;__proto__&#39; property of that object to refer to a different object. Simple, sweet and sexy.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thats probably all that I had to say in this post. I hope that you folks loved it.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
Signing Off!
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/06/naked-javascript-prototypes-and.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-7605472099311276441</guid><pubDate>Tue, 29 May 2012 18:08:00 +0000</pubDate><atom:updated>2012-06-02T15:48:25.720+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><category domain="http://www.blogger.com/atom/ns#">TODO</category><title>Naked JavaScript - Defining Classes and Function Instances</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;text-align: justify;&quot;&gt;In part 8 of the series &lt;/span&gt;&lt;b style=&quot;text-align: justify;&quot;&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Naked Javascript&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;span style=&quot;text-align: justify;&quot;&gt;, we are going to explore the mystical land of classes in Javascript. Now, coming from a backround with extensive programming in OOPS using Java, the absence of straightforward classes was quite a shocker to me at the start. The syntax, representation and implementation was a complete deviation from conventional methods.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As we learnt in the &lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;previous articles&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;, functions are the building blocks of numerous things in javascript. Classes are one of them. In fact, when you define a constructor function, what you are actually doing is creating a function that can be used to construct objects. Lets check out what I mean by looking at an example.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){
 console.log(&#39;&#39;);
 this.name = name;
 this.age=age;
}

var ancientNewJoinee = new Employee(&#39;xyz&#39;,99);

&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If you take a look at this function, there are a number of things that you will note about it.&lt;/div&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;We make use of the constructor naming convention.&amp;nbsp;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;We make use of the &#39;this&#39; keyword when assigning property values. The &#39;this&#39; keyword in javascript is one of those features that may sometimes lead you to lose all your hair if you dont know what you are dealing with(Personal advice : &#39;this&#39; is highly recommended for hairy women!). I tried to talk about it in intricate details in a previous article. (Make note, hairy women!)&amp;nbsp;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;We passed in two arguments and assigned them as properties on the &#39;this&#39; object.&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now, this might sound simple at first. Isi&#39;nt it what we always do in other languages like Java?? This is &#39;suppposed&#39; to be simple right??
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Hell no, its not.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Or maybe it is. Lets see what happens.
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Case 1 : Doing it the wrong way
&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You invoke the Employee function as if its just another function, instead of a constructor function. There is nobody who is ever going to stop you from doing such a hideous thing, right? So, lets do it and observe what happens.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){
 console.log(&#39;&#39;);
 this.name = name;
 this.age=age;
}

Employee(&#39;blah&#39;,90);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So what does this do? It works. It executes normally. Really cool, awesome. But what happend to the name and age property that I had passed as arguments? Whos kid did i rename?
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Turns out, that you did something totally horrible. What you did was, you created two new properties on the window object.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now, you should recollect from the previous articles that when you dont specify the context during a function invocation, the function is invoked using the context of the window object which takes the form is nothing but the &#39;this&#39; keyword inside the function. So, when you invoke the function by directly using the above syntax, you end up creating unnecessary properties on the window object. But thats not something that we want.
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Case 2 : Doing it the right way
&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You invoke the employee function by using the new operator.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){
 console.log(&#39;&#39;);
 this.name = name;
 this.age=age;
}

var ancientEmp = new Employee(&#39;xyz&#39;,99);
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you create an object in this way, you are actually creating a new function object, whose constructor is of type &#39;Employee&#39;, and it is this newly created function object that is then used as the context inside the constructor. So, when you are creating properties on the &#39;this&#39; variable, you are creating properties on your newly created function object. Since you have not explicitly specified any return value, the Employee constructor function returns the value of &#39;this&#39; variable from the function, which is nothing but the newly created function instance.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now that solves the mystery of creating classes. But there is much more to this scheme than meets the eye. How do you protect members. Like other programming languages, is there not a way to create private members that cannot be accessed from the outer world? There is a way, but it is not as obvious as it seems.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the example that we saw, we created two properties on the Employee instance. Both of these properties have public access. i.e. you can directly modify the values of these properties using the object access notation.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){
 console.log(&#39;&#39;);
 this.name = name;
 this.age=age;
}

var ancientEmp = new Employee(&#39;xyz&#39;,99);

ancientEmp.age=5;
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Oh no, now our ancient emp is younger than the kid next door. And child labor is a crime. We dont wana get into a mess with the authorities, do we? Well, at least not me. So, what do I do that enables me to access the properties on the object but still restrict me from modifying them to incorrect values.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The solution is simple. If you remember, in the &lt;a href=&quot;http://mycodefixes.blogspot.in/2012/05/javascript-closures-tutorial-examples.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;previous article, we spoke about closures&lt;/span&gt;&lt;/b&gt;&lt;/a&gt; and scoping in JavaScript. Scoping is a very powerful concept and this is one of the places where scoping is used to mimic the behaviour of private variables in classes.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The JavaBean standard is quite common in the Java world. We can apply the same principle to solve the problem of private variables using getters and setters for our variables and instead of accessing the properties directly.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){
 console.log(&#39;&#39;);
 this.getName = function (){
  return name;
 };
 this.getAge=function(){
  return age;
 };
 this.setName = function (newName){
  name=(newName==true)?newName:name;
 };
 this.setAge=function(newAge){
  age=newAge&amp;gt;14?newAge:age;
 };
}

var ancientEmp = new Employee(&#39;xyz&#39;,99);

ancientEmp.setName(&#39;&#39;);
ancientEmp.setAge(5);

console.log(ancientEmp.getName());
console.log(ancientEmp.getAge());
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The variables that are declared as function parameters are nothing but local variables in the function and behave like instance variables within the function scope. Due to javascript&#39;s scoping rules, they remain available to the function instance as long as the instance exists or are explicitly deleted from the object via some method. Creating getters and setters like this improves decoupling between the various code components.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now there is a minor issue with creating classes in this way. What happens is that whenever you create a function on an object you are creating a function ONLY for that object. Okey, lets think about it once more. In the first article of the series, I mentioned that when we create properties on objects, the properties are specific only to that object. Now look at our constructor function. What exactly is happening when we are trying to assign getters and setters on the &#39;this&#39; object? The getters and setters are created exclusively for the current function instance, i.e. the &#39;this&#39;. This implies that if you are creating a large number of objects, you will end up with each object having its own independent copy of each method and property.
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Although, it is highly desirable for each object of a class to be able to save the state of its properties independently, you really don&#39;t want to create multiple copies of the same function for each object because that will set your browser on fire and send your users screaming out of their houses. What you want to do is to define a function only once and let all the object point to that function. There are a couple of ways in which you can do that.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;1) Use an external function
&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function calculateSalary(){
 //Sorry for being so random!
 return Math.random();
}

function Employee(name, age){
 //Your stuff
 //
 //End of your stuff
 
 this.calculateSalary=calculateSalary();
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is a convenient and perhaps the simplest way that you can enable the reuse of the function. In this way,  the calculateSalary property of all the function instances will refer to the same externally defined calculateSalary function. Its simple, its nice. However, there lies scope of improvement. This brings to the point where we have to talk about perhaps the best and the coolest concept in javascript - prototypes (But that&#39;s just my opinion. So no bashing please!). I will be discussing javascript prototypes in the next article in absolute details. But until then, we need to learn just a bit to get this example working.
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;2) Using prototypes&lt;/u&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
What is a prototype?
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
A prototype is nothing but a property on an object that is visible to all instances of the object.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets demonstrate it in our class example. But before that, I want to reiterate a few points.
&lt;/div&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;Functions are objects themselves.&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;Functions can be instantiated to create special objects called function instances, by invoking them using the &#39;new&#39; keyword.&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;Function objects are also objects, but they have a special property called &#39;constructor&#39; that identifies the function that was resposible to instantiate this object.&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;Objects can have properties.&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;All objects have a property called &#39;prototype&#39;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The prototype property is pretty awesome. When you declare a property on the prototype property, it automatically becomes available to all the instances of that object/function/classs. This means that the prototype is the optimal place to declare a function that needs to be reused by all the objects of a class.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(name, age){

 this.name=name;
 //Other variables
}

Employee.prototype.getName=function(){
 return this.name;
}

Employee.prototype.setName=function(newName){
 name=(newName==true)?newName:name;
}
Employee.prototype.calculateSalary=function(){
 //Whoops!
 return Math.random();
}

var emp1 = new Employee(&#39;xyz&#39;);

console.log(emp1.getName());

&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I simplified the class just for the sake of the example. But if you have noticed, although we eliminated the issue of all the objects having a local vaiable pointing to the getter or setter, we are back to the problem where the property is defined on &#39;this&#39; itself. So, although this technique is useful for defining the calculateSalary function which does not make use of any properties, it somehow manages to breaks the principle of encapsulation because once again, all the properties will have to be publilcly visible for them to be accessible via the prototype method.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
That covers the basics of classes in Javascript. In the upcoming article, I plan to talk the most amazing prototype property and perhaps talk a bit about implementing inheritance in JavaScript. So, until then, stay tuned!

&lt;/div&gt;
&lt;br /&gt;
Signing Off!
&lt;br /&gt;
Ryan Sukale&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/05/javascript-classes-objects-tutorial.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-1366019718731238430</guid><pubDate>Sat, 19 May 2012 12:56:00 +0000</pubDate><atom:updated>2012-05-19T18:28:56.890+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Function Scopes, Hoisting And Other Quirks</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this part 7 of the series &lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Naked Javascript&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;, I am going to cover a few interesting quirks about functions and scoping in JavaScript and also talk about a strange feature called hoisting.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The first thing that one needs to know is that in JavaScript, we dont have block level scopes. Instead, in JavaScript, functions are responsible for creating new blocks of scope. Let me elaborate. In other programming languages like java, if you write a block of code like this

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public void someMethod(){
for(int i=i;i&amp;lt;10,i++){
 //Some usage of variable i
}
//Compiler error
System.out.println(i);
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you attempt to complie this program in Java, you are presented with an error at the line where you attempt to print the value of the variable &#39;i&#39;. Thats because, once you are outside the for loop, you cannot access the variable &#39;i&#39;. The scope of the variable &#39;i&#39; lies only within the code block in which it was declared in Java. Cominng to JavaScript, you need to understand that although javascript makes use of code blocks in the same way that Java does, the scope of a variable lies throughout the function in which it was declared. This means that the following code will successfully print 10 when executed in JavaScript.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;public void someMethod(){
for(int i=i;i&amp;lt;10,i++){
 //Some usage of variable i
}
//Prints 10
console.log(i);
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the above code, although we printed the value of the variable after it was declared and initialized ,but outside the curly braces, it worked perfectly fine. Now lets take another example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var n = 1;

function someRandomFunction() {
    console.log(n); // This prints 1.
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this function, we declared a variable outside the function. And were were easily able to print its value inside the function, due to closures. Now consider the following piece of code.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var n = 1;

function someRandomFunction() {
    console.log(n); // This prints undefined.
    var n = 100;
    console.log(n); // This prints 100
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Strange as it may seem, the first console ouput prints undefined. But why? Why is it not able to see the declaration of the variable &#39;n&#39; that was part of the closure like the earlier example? The reason behind this odd behavior is a feature called &#39;hoisting&#39;. Hoisting can be explained in a very simple way. For any variable declared within a scope, JavaScript separates the variable declaration and the variable initialization and hoists the declaration to the first line of the scope and initializes the variables at the appropriate lines at which they were initialized by the programmer.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Based upon the above explanation, this is what JavaScript saw when it ran our program.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var n = 1;

function someRandomFunction() {
 var n; //The variable declaration hides under harry potter&#39;s cloak. So you cant see it.
    console.log(n); // Now you know why its undefined here
    n = 100; //Now the variable gets initialized.
    console.log(n); // And prints out 100 here.
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see in the code above, the variable was undefined because it was shadowed by the local declaration of the variable in the function. (At least they got that part right!). But then, enter functions! And you have some more nasty code to deal with.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Check out the following piece of code wherein we declare a variable, a function using the conventional function declaration and a second function using the variable style declaration.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function hoistingContainer(){
 console.log(n);
 
 innerOne();
 innerTwo(); //Throws an exception
 
 var n = 100;
 
 function innerOne(){
  console.log(&#39;innerOne&#39;);
 }
 
 var innerTwo = function innerTwo(){
  console.log(&#39;innerTwo&#39;);
 }
 
 //This will execute but the code never
 //reaches here because of the previous exception
 innerTwo();
    
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the above example, make a note of the different ways in which we declared the two functions. If you run the above code, you would find that the first invocation of the function innnerTwo() leads to an exception whereas the invocation for the function innerOne executes normally. The only difference between these two functions was the way in which they were initialized. Perhaps it can be concluded that when functions are explicitly declared using a variable instead of the conventional notation, the initialization is not hoisted. However, using the conventional function declaration notation, even the function body gets hoisted along with the function declaration.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This implies that if you define functions using the standard notation instead of the variable notation, you can create functions anywhere within a scope and use them at any point within the same scope irrespective of their point of declaration.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The next interesting thing that I want to discuss about is what could happen if you accidentally forget to use the &#39;var&#39; keyword when declaring a variable inside a scope. Usually, when you declare local variables inside a function, you would use the var keyword. Only the variables that are declared using the var keyword become function local. However, if you accidentally forget the var keyword, javascript will not complain, instead it simply creates a property of the same name in the global scope i.e. on the &#39;window&#39; object. And as we are already aware, this can have disasterous consequences.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Take a look at the follwing code.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;window.constant = 10;

//prints 10, the constant from the global
//window object, 
console.log(constant);

function outerFunction(){

 var name = &#39;xyz&#39;;
 
 //Missing var keyword
 constant = 20;
}

//Invoke the function
outerFunction();

//Prints undefined since name was 
//locally scoped in the function
console.log(name);

//Prints 20, because its value was
//reassigned inside the function
console.log(constant);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Moral of the story, dont forget to use the &#39;var&#39; keyword when declaring local variables inside a scope.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;b&gt;Scopes and closures.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Closures take extensive advantage of the scoping feature of JavaScript. When a function is declared within the scope of another function, the inner function maintains &#39;hidden references&#39; to the varibles in the scope in which it was declared. You may also think of it as if functions have their own scope memory. Functions are behaviour. But Javascript functions can not only contain behaviour but also act as a data store. You may like to read &lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/2012/05/javascript-closures-tutorial-examples.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;span id=&quot;goog_1058968036&quot;&gt;&lt;/span&gt;another article&lt;/span&gt;&lt;span id=&quot;goog_1058968037&quot;&gt;&lt;/span&gt;&lt;/a&gt;&lt;/b&gt; of mine in which I have elaborated on closures with a couple of examples.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Semicolon insertion.&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is a really strange feature in javascript. The JavaScript processing engine tries to act smart and detect if a semicolon is missing where it should have been put. If it is found to be missing, it simply inserts the missing semicolon and executes the code without any warning. The simplest example, as demonstrated by many other is the problem of semicolon insertion with the return statement. The follwing code returns undefined.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function badStyle(){
 return
 {
  name:&#39;value&#39;
 };
}


function goodStyle(){
 return {
  name:&#39;value&#39;
 };
}

//Runs as expected
console.log(goodStyle());

//Prints undefined
console.log(badStyle());
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The second line prints undefined because, since javascript expected that return statement be terminated by a semicolon, it inserted one when it did not see any in the badStyle function. In the goodStyle function, it did not insert one because before the line termination, it encountered a symbol for object literal declaration.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Most of the problems that arise due to the above discussed quirks of the language can be avoided by writing simple yet decently formatted code. Variable declarations at the top, function declarations before using them, and proper opening and closing of curly braces and always adding a semi colon when you are terminating your statements wont only avoid these issues but also make your code readable and understandable.

Who knows, just spendinng just a couple of minutes restructuring your code may later save you hours of debugging.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Stay tuned. There&#39;s more to come!

&lt;/div&gt;
&lt;br /&gt;
Signing Off!&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/05/javascript-scopes-hoisting-examples.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-3226217322961666855</guid><pubDate>Sun, 13 May 2012 12:18:00 +0000</pubDate><atom:updated>2012-05-13T17:55:42.771+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Closure Dynamics</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In part 6 of the series &lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;Naked JavaScript&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;, I am going to discuss about the concept of closures in JavaScript. In the &lt;a href=&quot;http://mycodefixes.blogspot.in/2012/05/javascript-this-keyword-function.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;previous article&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;, I only briefly mentioned the topic of closures in JavaScript. In this article, we will drill down on the absolute details of this interesting topic. Closures in JavaScropt is a very important concept, and it comes in extremely handy when you are trying to innvoke functions or write classes. Not only is it one of the most important concepts in JavaScript, but it is also mesmerizingly interesting and contributes significantly to the expressive power of this wonderful programming language.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
We all know that every programming language has scoping rules for its variables. Javascript takes scoping to a whole new level in the form of closures. The beauty of this concept is most easily understood only by means of examples.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Consider a block of code that is written directly inside the script tag, i.e. in global scope.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:html&quot;&gt;&lt;script type=&quot;text/javascript&quot;&gt;
var bingo={name&quot;&#39;xyz&#39;};
console.log(bingo.name);
&lt;/script&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Since this block of code was written directly inside the script tag, the varible bingo becomes a global variable. Global variables are variables that are declared on the window object. In this case, since you have not specifed a parent object for bingo, the parent is assumed to be &#39;window&#39;.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This defaulting logic can cause a huge mess, as we learnt in the &lt;a href=&quot;http://mycodefixes.blogspot.in/2012/05/javascript-this-keyword-function.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;previous article&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;, because the size of a program graudally grows with time. And with new features coming in every now and then, you are very likely gonna want to name your variables based upon the real world concepts that they represent. However, declaring variables in the global scope can lead to serious problems because one function may end up reading values set by another function just because they use the same variable name. This is where scoping comes into the picture. However, scoping in JavaScript is a completely different beast. Unlike other programming languages like Java, JavaScript does not support block level scope.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
One solution to this problem would be to specify a different parent object for each variable having the same name. For example

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;//Defined on the global &#39;window&#39; object
var bingo={name:&#39;xyz&#39;};

//Defined in a nested object
var myObj = {};
myObj.bingo={name:&#39;xyz&#39;};
console.log(bingo.name);
console.log(myObj.bingo.name);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see, since we specified the name of the object inside another object, we were able to reuse a variable name without any conflicts. The varables bingo and myObj are global properties. i.e. they have global scope. What this implies is that for your own programs, you can create one single global object and define all your functions as properties of this object. In that way, you can ensure that you clutter the global scope with only a single object. So far so good.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now lets talk about functions.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you create a function, you frequently declare new variables inside a function to implement your logic. When these variables are declared, they are declared with a scope that spans the braces that enclose the function. Lets see an example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function popeye(){
 var msg = &#39;I love olive&#39;;
 console.log(msg);
};

function bluto(){
 var msg = &#39;I love olive too!&#39;;
 console.log(msg);
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see, in our function we were able to reuse the variable &#39;msg&#39; just because they existed in different scopes. Even this is pretty simple, and of course, works as expected. But wait, there&#39;s more.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Enter nested functons.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Unlike Java, javascript lets you create nested functions. The syntax of declaring a nested functions is the same as declaring a normal function. Lets see an example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function popeye(){
 var msg = &#39;I love olive&#39;;
 
 function printMe(){
  console.log(msg);
 }
 
 printMe();
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Even this was easy. We just created a new function inside the popeye function and invoked it. But note that you were able to access the value of the variable &#39;msg&#39; even thought you did not declare it inside the printMe function. So this tells you that nested functions have access to the variables declared in the scope of the outer function. And this nesting can continue further down.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function slimShady(){
 var msg = &#39;I am slim shady&#39;;
 
 function iAmSlimShady(){
  
  function theRealSlimShady(){
   console.log(msg);
  };
  theRealSlimShady();
  
 };
 
 iAmSlimShady();
};
slimShady();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Ok, even this is pretty easy to understand. We create a function slim shady, which declares and invokes an inner function iAmSlimShady which again declares and creates an inner function theRealSlimShady. But the story does not end here. Irrespective of the depth of nesting, an inner function is able to access the variable declared in the outer function. This is what a closure is all about. The ability of an inner function, to dynamically access the values of variables declared in the enclosing function even after the enclosing function has completed its execution is called a closure. The concept of closures actually becomes blatantly obvious when inner function references are accessed from outside the context of the method in which they were declared. Consider this example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function printer(msg){
 
 function printMe(){
   console.log(msg);
 };
 
 return printMe;
};
var p = printer(&#39;Popeye says - Olive is mine&#39;);
var b = printer(&#39;Bluto says - Your Olive is mine&#39;);

p();
b();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
What do you see in your console?? What were you expecting? If you have read the other parts of the series, then you will know that I had mentioed earlier that functions are nothing but objects. So, we can simply return a reference of an inner functon from the outer function and thats exactly what we have done. We returned the printMe function from the printer function. Thereby, in our case, both the functions p() and b() refer to the same function i.e. the inner function printMe.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the first invocation of the function function printer i.e. p, we passed in different string for popeye. In the second invocation, we passed in a string for bluto. And since both p and b refer to the same function - printMe, it may feel that both p() and b() should print the latest value of &#39;msg&#39; that was passed to the outer function.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Sadly.. Or maybe gladly, thats not what happens. As it turns out, variables declared in an outer function can be accessed by the innner function even outside of the lifetime of the outer function. So even though, upon invocation of the printer function with the argument for popeye&#39;s claim, and code execution is completed for the outer function, the inner function will retain a hidden reference to the state of the variable in the outer function. In our case, the variable in the outer function was nothing but the &#39;msg&#39; argument, which is nothing but a local variable in the printer function.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This implies that if you can preserve the reference to the inner function, you can artificially increase the life of a local variable that was declared in the outer function.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The game of closures gets a bit trickier when used in conjunction with the &#39;this&#39; keyword. Lets see how that happens using an example similar to the one that we saw above.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function outer(name){
 
 this.name = name;
 console.log(&#39;Outer name : &#39; + this.name);
 
 function inner(){
   console.log(&quot;Inner Name : &quot; + this.name);
 };
 
 inner();
};
outer(&#39;Popeye&#39;);
outer(&#39;Bluto&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Observe closely. When I invoked the outer function without specifyng a context, the &#39;this&#39; keyword in the outer function references the window object and assigns a value of &#39;Popeye&#39; to the name property of the window object. Even when the inner function is invoked, no context is specified at invocation time, the value of &#39;this&#39; is also set to the global window object and we can simply print the name.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now, lets twist this example a bit.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var holder = {};
holder.outer =function (name){
 
 this.name = name;
 console.log(&#39;Outer name : &#39; + this.name);
 
 function inner(){
   console.log(&quot;Inner Name : &quot; + this.name);
 };
 
 inner();
};
holder.outer(&#39;Popeye&#39;);
holder.outer(&#39;Bluto&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The only difference in this case was that instead of defining the outer function as a global function, we declared it as a function on the &#39;holder&#39; object. This sublte change brought about a drastic change in the output. The inner function does not print anything this time. This is what happens instead.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you are in the outer function, the &#39;this&#39; refers to the holder object. And you set a name property on it. Simple. But when you invoke the inner function, since you haven&#39;t specified any context, the this inside the inner function refers to the window object. And since you have not set any value for a property called name in the window object, all you get is an undefined value.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets see another variation of the above example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var holder = {};
holder.outer =function (name){
 
 console.log(&#39;Outer name : &#39; + name);
 
 function inner(){
   console.log(&quot;Inner Name : &quot; + name);
 };
 
 inner();
};
holder.outer(&#39;Popeye&#39;);
holder.outer(&#39;Bluto&#39;);

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this case we have removed this &#39;this&#39; keyword. So both the functions are directly referencing the value of the &#39;name&#39; argument in the outer function. The inner function is able to access the &#39;name&#39; variable because of the closure. This can still be made more fun by harnessing the real power of closures.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var holder = {};
holder.outer =function (name){
 
 console.log(&#39;Outer name : &#39; + name);
 
 function inner(){
   console.log(&quot;Inner Name : &quot; + name);
 };
 
 return inner;
};
var p = holder.outer(&#39;Popeye&#39;);
var b = holder.outer(&#39;Bluto&#39;);
console.log(&#39;Invoking Inner functions&#39;);
p();
b();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Take a look at the changes made. First of all, the function returns a function reference of the inner function. So now, both the objects p, and b refer to the same inner function. When you create p using the parameter &#39;Popeye&#39; the local variable &#39;name&#39; is set to &#39;Popeye&#39;. And thats the value that is accessible from the inner function. But observe that when you invoke the function with the parameter &#39;Bluto&#39; the value of the local variable has CHANGED. This is an important point to note.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Later when you invoke the function p(), you are still able to access the value that was passed when the value of &#39;name&#39; was &#39;Popeye&#39;. This is what closures do. When you are in a closure, the inner function is able to &#39;remember&#39; the old value of the outer function that was in its scope. And whenever the inner function is invoked, it works on the reference to the same outer value. So, if you have multiple references to the same inner function that acccesses the same outer value, you can effectively write code as if the values accesed in the inner function are different for different invocations of the same function, as if they are in completely different scopes.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The only variable for which closures is not effective is the &#39;this&#39; keyword. Thats because each functions has a different value of the &#39;this&#39; keyword that solely depends upon the way in which the function was invoked. But what if you want to make use of the &#39;this&#39; reference of your outer functon inside your inner function? Pretty simple. You wrap it in a closure variable. See example below.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var holder = {};
holder.outer =function (name){

 this.name=name;
 var that = this;
 console.log(&#39;Outer name : &#39; + name);
 
 function inner(){
   console.log(&quot;Inner Name : &quot; + that.name);
 };
 
 return inner;
};

var p = holder.outer(&#39;Popeye&#39;);
p();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
That was pretty easy. We just created a local variable &#39;that&#39; and assigned the value of &#39;this&#39; to that. And thats how the cookie crumbles!
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this article we saw the several different examples of closures. Closures are widely used in almost any piece of code that you read about these days so its important to have a thorough understanding of this concept. 

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thats all that I have for this article. I hope you found it useful. There are still a couple of interesting things I intend to cover in the upcoming articles of this series. If you feel lost somewhere, go and check out the previous parts of &lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;b&gt;this series&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;. If not, stay tuned for more!
&lt;/div&gt;
&lt;br /&gt;
Signing off
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/05/javascript-closures-tutorial-examples.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-2005340619480582365</guid><pubDate>Wed, 09 May 2012 19:21:00 +0000</pubDate><atom:updated>2012-05-10T00:59:20.083+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Demystifying The this Keyword and Function Contexts</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In part 5 of the series &lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;Naked JavaScript&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;, I am going to focus on the the usage of the &#39;this&#39; keyword in JavaScript. It is extremely important to correctly understand the &#39;this&#39; keyword it is one of those features of JavaScript where even a small and innocent looking mistake can literally make you pull out your hair if you don&#39;t know what you are dealing with.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
JavaScript, unlike the name, is not a script like version of Java. In fact, its a completely different beast. And even if it contains keywords and features that &#39;look&#39; like Java, it does not follow the programming conventions of the the same. I believe that the this keyword is one of those perfect examples of JavaScript where almost everyone new to the language is likely to get confused if they have prior experience of working &amp;nbsp;with other languages, and therefore make silly mistakes. Some of which can be completely devastating and lead to complicated bugs and sleepless nights.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets begin with a simple example example we create an object as follows

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var myHappyObject = {
 name : &#39;MrHappy&#39;,
 echo : function (){
  console.log(this.name);
 }
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now this seems like a perfectly nice object with a happy little fuction that echoes the value of the &#39;name&#39; property. But hey! dont forget that &#39;echo&#39; is nothing but an object that holds a reference to a function. And being a reference, it can be passed to a completely different object. For example, you could do something like this.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var duplicateReference = myHappyObject.echo;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Note that I did not invoke the echo function. I only used a reference to the echo function and assigned it to the variable &#39;duplicateReference&#39;. After this line, the echo object and the duplicateReference object, both point to the same function that prints the value of the &#39;this.name&#39; inside the function. But there is a small (or should I say, HUGE) difference! Take a look at the following code.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;myHappyObject.echo();
duplicateReference();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Note the output on your console. While the first invocation correctly printed the value &#39;xyz&#39;, the second invocation simply crash landed and printed undefined. At first glance, you might wonder why this is so? Do they not point to the same function? Of course they do. Then why the difference?

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In order to understand how the above code works, you need to understand the underlying mechanics of the &#39;this&#39; keyword and play around with the concept of function contexts.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
By default, whenever you invoke a function, a function is invoked in the context of the window object.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function outerFunc(){

 console.log(&#39;outer &#39; + this);

 function innerFunc(){
  console.log(&#39;inner &#39; + this);
 }
 
 innerFunc();
}

outerFunc();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you invoke the outer function, the first line of console.log prints the value of the window object. That happens because, by default, if you don&#39;t specify an object during invocation time, the function call is assumed to be something like window.&lt;yourfunctionname&gt;()&lt;functionname&gt;. Hence when you are inside the outerFunc, the context is set to the window object, i.e. the value of the &#39;this&#39; variable is set to the window object. And thats how you get the value of the window in the first line of console.log.&amp;nbsp;&lt;/functionname&gt;&lt;/yourfunctionname&gt;&lt;/div&gt;
&lt;functionname&gt;&lt;/functionname&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Similarly, in the second console.log, inside the innerFunc, the value of &#39;this&#39; is printed as the window object.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets take another example.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = {};

bingo.outerFunc=function(){

 console.log(&#39;outer &#39; + this);

 function innerFunc(){
  console.log(&#39;inner &#39; + this);
 }
 
 innerFunc();
}

bingo.outerFunc();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This time we defined the outerFunc as a function on the bingo object. The direct implication of this is that you cannot invoke the function directly. i.e. you cannot directly say outerFunc(). Why? That&#39;s because if you do so, the javascript engine will search the window object for the outerFunc() function and since you have defined the function on the bingo object, the search will fail and you get a big slap in the face. However, if you have an itch (in all the wrong places) and still feel like messing with things, you can always invoke it by writing the following code&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;window.bingo.outerFunc();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The above example also proves that all objects that seem to be orphaned are not actually orphaned. And that the window object has a strong inclination to play god to all of them.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
But if you notice the console messages, you will also note that even though the outerFunc prints the bingo object as the value of &#39;this&#39;, the innerFunc still prints the window object as the value of &#39;this&#39;. This implies that context is not inherited. i.e. when functions that are defined inside of a function and invoked inside the function itself, they are oblivious of the value of the &#39;this&#39; keyword in the outer function. Instead, they get their own copy of the &#39;this&#39; keyword which depends solely on the way in which the inner function was invoked.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This odd behavior can have serious implications if not understood properly.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets take a look at our previous example of myHappyObject. Lets redefine the object so that it looks something like this.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var myHappyObject = {
 name : &#39;MrHappy&#39;,
 rename : function (name){
  this.name = name;
  console.log(this.name);
 }
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets try to do the same things that we did earlier and create another reference to the rename function.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var duplicateReference = myHappyObject.rename;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So far so good. But there is something very nasty in the piece of code that follows as a consequence of the above assignment. Lets try to invoke both the function references and see what happens.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;myHappyObject.rename(&#39;abc&#39;);
duplicateReference(&#39;garbage&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The code executes smoothly without any errors or warning. It even renames the &#39;name&#39; property of the myHappyObject to &#39;abc&#39;. Thats what the first line of code does. But then, what does our second invocation of the duplicateReference do? This function also sets the &#39;name&#39; property of an object to &#39;garbage&#39;. But for which object does it set the name property?? I hope that you might have guessed the answer by now. Its our very own window object. What happened over here is something very evil. I say evil because it might not have been your intention to add garbage to the window object but because of the way in which the code was invoked, you accidentally created a property called &#39;name&#39; on the window object and assigned to it the value &#39;garbage&#39;.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
What if there was already an existing property called &#39;name&#39; on the window object and it was being used elsewhere. Our little piece of code would probably lead to a bug that would have been extremely difficult to trace. So remember that whenever you make use of the &#39;this&#39; keywords, pay careful attention to the manner in which the functions are being invoked.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Another point to be noted is that although we created a duplicateReference to the rename function, you cannot invoke it using myHappyObject.duplicateReference(&#39;abc&#39;). The reason is simple. The myHappyObject does not have any property called duplicateReference. Hence the invocation is invalid. However you can always do something like the following code&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;duplicateReference.apply(myHappyObject,[&#39;yay&#39;])

//The above code is equivalent to
//myHappyObject.rename(&#39;yay&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The code in this case becomes equivalent because when you make use of the apply function, the first parameter behaves as the context of the function and hence is the value of the &#39;this&#39; keyword inside the function.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Also there was a case where we talked about the inability of an inner function to access the value of the &#39;this&#39; keyword present in the outer function. That statement, however, is not entirely true. JavaScript has a extremely beautiful feature called closures that makes so many things possible that it makes you go both wild with madness as well as excitement at the same time. And that&#39;s what I am going to talk about in the next part of this series.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So stay tuned, and stay awake.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
For, the fun....&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Has only just begun. ;)&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
Signing Off
&lt;br /&gt;
&lt;br /&gt;
Ryan

&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/05/javascript-this-keyword-function.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-4730916416350532380</guid><pubDate>Wed, 02 May 2012 16:50:00 +0000</pubDate><atom:updated>2012-05-10T00:59:36.839+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Constructor Functions</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the part 4 of the series - &lt;b&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span id=&quot;goog_1812900982&quot;&gt;&lt;/span&gt;Naked Javascript&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;span id=&quot;goog_1812900983&quot;&gt;&lt;/span&gt;, we are going to take a deeper look at Functions in Javscript and explore its twisted syntax and various implications. In the &lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/2012/04/javascript-functions-apply-call-example.html&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;previous article&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;, we learnt about the different ways in which you can invoke functions. In this part, we examine the stuff that is responsible for creating functions in JavaScript.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Functions in JavaScript are fantastic. Not only are they used to execute functionality, but they are also used to create other objects and functions(Yes, you read that right). Functions are means by which you implement a very significant feature of the language - inheritance. We will be talking about the details of inheritance later. For now, lets continue exploring functions.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Like all amazing and awesome things in the world, when you talk about functions in Javascript, there is more to it than meets the eye. The real way to figure out what more is out there is by firing up your browser console and dissecting it bit by bit.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets first try to answer a very basic quesion. What exactly is a function?
Well, a function is nothing but a Javascript object. Yes I know that I have said that numerous times in the previous parts of the series, but believe me when I say that it is impossible to stress this point any less.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
However, functions are objects with a few special features that makes them stand out from ordinary objects. In order to understand what functions are you first need to know who creates functions.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In javascript, in order to create objects, all you need to do, is define a constructor. Unlike other programming languages(e.g. Java), you dont need any special wrapper around a function, i.e. your constructor does not have to defined inside of a class. A constructor is just an ordinary function. For example
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(){
 //blah blah
 console.log(&#39;Employee function&#39;);
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is a simple constructor function. There is nothing special about this function. The only thing that is different here is the fact that we followed a different naming convention by capitalizing the first alphabet of the function name. This convention is recommended and important when you are declaring constructor functions because it lets others differentiate between ordinary functions and functions that are designed with the intention of be used as a constructor function.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now this function does not seem to be doing anything special. But in Javascript, you can do something totally strange. You can create instances of functions. For example, once you have declared a employee constructor function like the one above, you can do somethig like this.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(){
 console.log(&#39;Employee function&#39;);
};

var emp1 = new Employee();
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The syntax is both befuddling and amusing. Lets try to understand the what exactly is happening under the hood here when we invoke a function along with a new operator.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This syntax of invoking functions that are meant to be constructor functions was designed to appeal to Java programmers. When you invoke a function using the &#39;new&#39; keyword, the runtime creates an empty object and passes that object to the function. That object then becomes the value of the &#39;this&#39; keyword inside the function. I will be covering the &#39;this&#39; keyword in JavaScript in details in a later part of this series. But for now, assume that when invoked using the &#39;new&#39; keyword, the &#39;this&#39; inside the function behaves similar to the &#39;this&#39; keyword used in Java. Once the function finishes its execution, the function automatically returns a reference to the &#39;this&#39; even though you did you explicitly write a return statement at the end of the function. Note that had you invoked the Employee() function without the &#39;new&#39; keyword, no new object would have been created, and our Employee function would have returned nothing without an explicit return statement.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
After the function execution, the variable emp1 now points to the newly created object. You can think of this object to be an instance of the function. I say so because if you try to execute &#39;emp1 instanceof Employee&#39;, it returns true. The &#39;instanceof&#39; keyword is an easy way to determine the name of the function responsible for constructing an object.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Not only that, every object has access to a property called &#39;constructor&#39; that tells you about the constructor of that object. For example, you could do something like this.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(){
 console.log(&#39;Executing Employee function&#39;);
};

var emp1 = new Employee();
var dummy = {};

console.log(dummy.constructor);
console.log(emp1.constructor);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
When you run it on chrome, you would be able to see the following lines on the console.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;console-message console-log-level&quot; style=&quot;background-color: white; border-bottom-color: rgb(240, 240, 240); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; font-family: Consolas, &#39;Lucida Console&#39;, monospace; font-size: 12px; margin-left: 24px; min-height: 16px; padding-bottom: 1px; padding-left: 0px; padding-right: 22px; padding-top: 1px; position: relative; text-align: -webkit-auto;&quot;&gt;
&lt;span class=&quot;console-message-text source-code&quot; style=&quot;box-sizing: border-box; white-space: pre-wrap;&quot;&gt;Executing Employee function&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;console-message console-log-level&quot; style=&quot;background-color: white; border-bottom-color: rgb(240, 240, 240); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; font-family: Consolas, &#39;Lucida Console&#39;, monospace; font-size: 12px; margin-left: 24px; min-height: 16px; padding-bottom: 1px; padding-left: 0px; padding-right: 22px; padding-top: 1px; position: relative; text-align: -webkit-auto;&quot;&gt;
&lt;span class=&quot;console-message-text source-code&quot; style=&quot;box-sizing: border-box; white-space: pre-wrap;&quot;&gt;function Object() { [native code] }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;console-message console-log-level&quot; style=&quot;background-color: white; border-bottom-color: rgb(240, 240, 240); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; font-family: Consolas, &#39;Lucida Console&#39;, monospace; font-size: 12px; margin-left: 24px; min-height: 16px; padding-bottom: 1px; padding-left: 0px; padding-right: 22px; padding-top: 1px; position: relative; text-align: -webkit-auto;&quot;&gt;
&lt;span class=&quot;console-message-text source-code&quot; style=&quot;box-sizing: border-box; white-space: pre-wrap;&quot;&gt;function Employee(){
 console.log(&#39;Executing Employee function&#39;);
}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The first line is the normal console.log that was executed when you created a new function object. Thats not much of a mystery anyways. But then, you see another line that says &quot;function Object() { [native code] }&quot;. This comes from the log of dummy.constructor. And then in the next line, you see all the dirty little details of our own &#39;Employee&#39; function. (This point should give you ideas!).
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Ok, now that we understand how to create a custom constructor function and create instances using the constructor function, lets have some more fun with functions.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Remember when I said that, that functions are objects too?. And this directly implies that there has to be some constructor function that constructs functions(Tongue twister alert!). Dare to explore? Yes sir!
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function Employee(){
 console.log(&#39;Executing Employee function&#39;);
};

console.log(Employee.constructor);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Turns out that there is a function called &quot;Function&quot; that does the magic of constructing functions for us(This should be your Eureka moment, ryt!! Or, maybe its just me).
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
So we have a function that creates other functions and it is aptly a function called - &quot;Function&quot;. Also note that the naming convention for a constructor function is being followed here. Hey, guess what? Even anonymous functions have the same constructor. Of course they do, since they are functions too(Love that rhyme, dont you?).
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;console.log((function noname(){}).constructor);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Well, am gonna let curiosity kill the cat and find out who makes the Function function too, because hey, even thats an object, ryt?
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;console.log(Function.constructor);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
It seems that the Function function creates itself. (Ok, now the cat&#39;s dead &lt;sob sob=&quot;&quot;&gt;).&amp;nbsp;&lt;/sob&gt;&lt;/div&gt;
&lt;sob sob=&quot;&quot;&gt;&lt;/sob&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now that you know that a function creates a function, this gives you the ability to formulate a new syntax to create functions.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var dynamicFunction = new Function();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
However, the function that we just created is not of much use in this way, because we don&#39;t have a function body. But hey ho!, you can create your function with full fledged parameters and the function body as shown in the example below.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var nowThisIsWeirdButCool = new Function(&#39;p1&#39;,&#39;p2&#39;,&#39;console.log(&quot;Stripping &quot;+p2+&quot; &quot;+p1)&#39;);
nowThisIsWeirdButCool(&#39;Naked&#39;,&#39;Javascript&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you can see, this is a pretty convoluted way of defining functions, but still, it certainly is one of the ways in which you can choose to create functions in JavaScript. Amazing right? But, note that is a really BAD BAD BAD way to create functions because functions created using this technique make use of the sacred JavaScript function &#39;eval&#39; in order to evaluate the definition of the function body provided as a string parameter at runtime. The use of the eval() function is considered to be evil. ( Need I say that the two words even rhyme to a certain extent. See my point?).&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
To conclude, this post uncovered a few more aspects of functions in JavaScript. As I said before, functions are the building blocks of so many things in JavaScript, that is is quite impossible to talk about anything in JavaScript without talking about functions and their various idiosyncracies.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
However, this is all that I have for this part of the series. There&#39;s more coming. Stay tuned for the next!&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/05/constructing-objects-javascript.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-6934494271965742192</guid><pubDate>Wed, 25 Apr 2012 17:29:00 +0000</pubDate><atom:updated>2012-04-25T23:00:20.794+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Function Access And Invocation</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In part 3 of the series &lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;Naked Javascript&lt;/a&gt; &lt;/b&gt;&lt;/span&gt;, we are going to talk a little more about functions in Javascript. In the &lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/2012/04/naked-javascript-function-basics.html&quot; target=&quot;_blank&quot;&gt;previous article&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;, we just got a basic introduction to JavaScript functions. In this part and the parts that follow, we are going to rip them apart.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Ok, the first thing that you need to know is that - Functions are Awesome. And thats with a capital A(and a capital F!).
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In Javascript, functions are first class objects. A function is an object with a special ability - the ablility to be invoked. There are basically 4 ways in which you can invoke a function.
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;i&gt;1) The usual way&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function boring(msg){
 console.log(&#39;oh, this is so so conventional &#39;+ msg);
};

boring(&#39;You are killing me!&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;2) The unusual way - anonymously&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;(function cool(msg){
 cool(&#39;Do you think that I am cool? &#39; + );
})(&#39;Totally!&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #990000;&quot;&gt;3) Using the Call method
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function callMe(msg){
 console.log(&#39;Thanks for calling us &#39; + msg);
}

callMe.call(this, &#39;Yea, right..&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;text-align: justify;&quot;&gt;The syntax for the call is pretty straightforward. The first argument is the context in which the function is to be invoked. The remanining arguments, that you provide as comma separated values is are sent as arguments to the function.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #990000;&quot;&gt;4) Using the Apply method
&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function applyMe(msg){
 console.log(&#39;Do you want me to apply something? &#39; + msg);
}

applyMe.apply(this,[&#39;yes&#39;]);
&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;text-align: justify;&quot;&gt;This syntax is similar to the call syntax. The only difference being the fact that you need to pass an array as the parameter to the function instead of a comma separated list as you did in the syntax for call.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
We saw the first two techniques in the previous post. But what makes the last 2 techniques so awesome? The answer lies in understanding the concept of a function context and a better understanding of the syntax of accessing properties in Javascript.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets talk about the object syntax first.
As we saw earlier, functions can be declared on objects as if they are normal properties. In fact, they are normal properties(albeit on steroids, but thats a secret...). Now, the world and its mother knows that one can access properties of objects using the dot operator. But thats convetional and not to mention, dead booring(because we do it all the time). Javascript to the rescue! Javascripts makes things a bit more interesting for us by allowing you to access objects using a different syntax, like a map. For example, check out the code below
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = {name:&#39;xyx&#39;, desc:&#39;I am Mr Bingo&#39;};

//The uncool way of accessing a property of an object
console.log(bingo.name);
//The cool way of accessing a property of an object
console.log(bingo[&#39;desc&#39;]);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Note the special syntax that we used to access the desc property. We simply used the property name as a string in inside the brackets and were able to access the value of the property. This means that objects can be accessed as if they are key-value maps. This is a really cool feature when it comes to functions because functions are objects too. And that means that even they can be accessed via the same technique. This makes it possible to write something like this

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo={};
bingo.sayHello=function(){
 console.log(&#39;hello&#39;);
};

bingo.shout=function(){
 console.log(&#39;Scream at the top of my lungs&#39;);
};

function doAsISay(funcName){
 bingo[funcName]();
}

doAsISay(&#39;sayHello&#39;);
doAsISay(&#39;shout&#39;);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Did you see that!! You can dyanmically pass the function name as a parameter. So you can invoke a function at runtime and not even need to know its name. The doAsISay function acts as a function invoker and invokes any function that is available on the bingo object.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
An advantage of using the map syntax for accesing object properties is that it lets you use reserved keywords as keys without the runtime complaining about it. Althouth, I dont think it makes good sense to use keywords as object properties since no matter what programming background you come from, watching a keyword being used anywhere always tends to trigger a train of thought that may not be on the same track of the program that you are working on. However, in a few corner cases, if you do find the need to use reserved keywords as object keys, you are more than free to do so.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
We are still not done with functions. Functions are the heart of Javascript, and like all matters of the heart, this one will take its time too. In the next article, I am going to contine with functions and give them another shot to exhibit their awesomeness.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Happy Fungramming!
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/04/javascript-functions-apply-call-example.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-4674266504057832673</guid><pubDate>Wed, 18 Apr 2012 06:44:00 +0000</pubDate><atom:updated>2012-04-18T12:18:57.316+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Function Basics</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
After talking about the basics of objects in the &lt;a href=&quot;http://mycodefixes.blogspot.in/2012/04/naked-javascript-object-basics.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;previous article&lt;/span&gt;&lt;/b&gt;&lt;/a&gt;, in part 2 of this series - &lt;b&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;Naked Javascript&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;, we will discuss a little more about objects, their properties and take a sneak peak at functions in javascript.
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As we learnt in the previous post, in Javascript you can create objects of the same class but each object can have a different set or properties. Properties can be defined on javascript objects even after they have been declared and used any number of times. For example, you can do this in javascript.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = {name:&#39;xyz&#39;};
bingo.says = &#39;hello&#39;;
bingo.volume = 5;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see, you can add properties to a javascript object even after the object has been declared and initialzied. This is something that cannot be done in Java because each object in Java MUST confirm to the structure of the class of which it is an instance. This feature of being able to dynamically create new properties for existing objects in javascript is called expandos.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If javascript allows you to create expando properties, it also lets you delete them dynamically. Here&#39;s how you would delete a property from a javascript object.
&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;delete bingo.volume;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The above statement will delete the volume property on the bingo object thereby clearing the memory occpied by the object&#39;s value.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Not only can you declare properties on javascript objects, but you can also declare functions. It would&#39;nt be too far fetched to say that functions are the actual building blocks of javascript.

&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
Functions can be defined in Javascript in a couple of ways.

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;1) &lt;span style=&quot;color: #e06666;&quot;&gt;&lt;i&gt;The not so cool way

&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function doSomethingCool(){
 console.log(&#39;I do something mindblowingly cool&#39;);
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;2) &lt;span style=&quot;color: #e06666;&quot;&gt;&lt;i&gt;The cool way - Anonymous functions

&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function (){
 console.log(&#39;My parents forgot to name me!&#39;);
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you see, an anonymous function has no name(and hence the name &#39;anonymous&#39; functions! Now that kinda reminds me of the saying - those that have nobody have god). There are many ways in which you can use anonymous functions. But before you start using functions - anonymous or non anonymous, you need to make a clear mental note of one point - Like everything else in javascript, functions are objects too! which means that you can assign it to a variable(huh?).

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Lets try to build upon the above 2 examples annd try to assign these functions to variables.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;function doSomethingCool(){
 console.log(&#39;I do something cool&#39;);
}

var makeSomeNoise = doSomethingCool;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Oh! Scary! Well, if you&#39;ve ever worked with Java, then it might be. However there are some similarities between the two languages. When you assign the function doSomethingCool to the variable makeSomeNoise, you are simply creating another reference to the function. Now both the objects - makeSomeNoise and doSomethingCool refer to the same function. That is why, if we wished to, we could rewrite the first Method of function declaration using the second syntax! See the code below

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var doSomethingCool = function(){
 console.log(&#39;I do something cool&#39;);
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This way of declaring the function doSomethingCool is exactly equal to the Method 1, the only difference being the syntax. The point to be noted here is that a function name is actually nothing but a variable declared in the local scope.

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Although the &#39;not so cool way&#39; of defining functions is extremely convenient to use, the above syntax in which you assign a variable to an anonymous function makes it extremely easy to always remember that a method name is nothig but a javascript object, albeit not an ordinary one. I will elaborate more about that in the upcoming articles.

&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Now that you know that function names are actually objects, you can do some weird stuff with it. Something that you could have never do using Java. For example, take a look at the code below

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;//Define the function
function doSomethingCool(){
 console.log(&#39;I do something cool&#39;);
}

//Invoke the function
doSomethigCool();

//Assign the function name to point to a different function
doSomethingCool = function(){
 console.log(&#39;I do something hot&#39;);
}

//Invoke the function again
//This time the function will print a different message
doSomethingCool();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Check your browser&#39;s console to verify the output. (Just in case you dont know, if you are using Google Chrome you can press F12 or Ctrl+Shift+I to open the chrome developer tools and click on the &quot;console&quot; tab. If you are using Firefox, you need to install the Firebug extension and then you can press F12 to get the firebug console and then select the &quot;console&quot; tab to view the console messages)

&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Another cool thing that you might want to know about functions is the way in which it is invoked. In the above examples, we created a few functions and assigned names to them. After that we were able to invoke them using the ordinary function invocation syntax. But what do we do if we want to invoke an anonymous function? The solution is simple(and interesting!)

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;(function(){
 console.log(&#39;I am anonymous&#39;);
}) ();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Notice the braces that enclose the anonymous function definition and the bracecs that follow the function name. Using the above syntax, you can invoke not only an anonymous function, but any function. lets see an example.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var usefulStuff = (function makeUsefulStuff(){
 var bingo={desc:&#39;My name is Bingo&#39;};
 return bingo;
})();
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Observe the syntax closely. I have simply created a function and invoked it. Inside the function, I created a local variable in the function called &#39;bingo&#39; and that&#39;s what I returned from the function. The immediate function invocation causes the return statement to execute. So, now, the variable usefulStuff and the variable bingo point to the same object. Note that in this case, just because we invoked the function immediately, usefulStuff does not reference a function, instead it references an ordinary object that was returned by the function. Its is quite possible that when reading someone else&#39;s code you might forget to read the braces. After all, they are so tiny and close together. But, just in case you find yourself expecting one object and actually getting another, remember to look for a sneaky pair of braces somwhere after the function defintion and you might just save yourself a buttload of hair.

&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Coming back to the topic of declaring functions as object properties, the sytnax is pretty simple. Check out the sample code below&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;//Create an object;
var coolObject ={};

//Declare a function on the object
coolObject.doSomethingCool=function(){
 console.log(&#39;Do something cool&#39;);
};
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Although we say that we are declaring a function on an object, we are actually doing nothing but creating a new property on the object that refers to a function definition. Which means that you can even do the following.

&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var coolObject ={};

//Declare a function on the object
coolObject.doSomethingCool=function(){
 console.log(&#39;Do something cool&#39;);
};

coolObject.doSomethingCool();

//What!! You can even make it a string now! No way!
//But apparently you can!
coolObject.doSomethingCool = &#39;I am a string&#39;;

console.log(coolObject.doSomethingCool);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This seems to work because the property &#39;doSomethingCool&#39; was nothing but a reference to a function. We can reassign it to anything that we wish to at any point of time.
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Well, that covers a few nuances of functions. But there is a lot more cool stuff that we will uncover in the upcoming articles! Stay tuned!
&lt;/div&gt;
&lt;br /&gt;
Happy Fungramming!
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/04/naked-javascript-function-basics.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-273251812433027079</guid><pubDate>Tue, 10 Apr 2012 19:44:00 +0000</pubDate><atom:updated>2012-04-29T21:43:13.412+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">javascript</category><category domain="http://www.blogger.com/atom/ns#">NAKED JAVASCRIPT</category><title>Naked JavaScript - Object Basics</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In this series- &lt;b&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;u&gt;&lt;a href=&quot;http://mycodefixes.blogspot.in/p/series.html#nakedjavascript&quot; target=&quot;_blank&quot;&gt;Naked JavaScript&lt;/a&gt;&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;, we are going to delve into the gory details of javascript and extensively elaborate on its features like objects, functions, classes, inheritance, prototypes and closures.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
In part 1 of this series, I am going to talk about objects in javascript. And since almost everything in javascript is an object, maybe I can say that I am practically talking about the basic fundamentals of the language. In one way, this part is just a foundation for all the rest of the articles that follow&amp;nbsp;in this series.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Javascript deals heavily with objects. It deals with them at so many levels that sometimes its unfathomable if you are used to writing code in &#39;conventional&#39; languages like Java or C or C#. Everything from functions to inheritance to arrays take on a completely different perspective in Javascript and this makes the language &amp;nbsp;unpredictable for a to programmers from a different programming background.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Before we begin, there is one important statement that I want to start with. And that is &#39;&lt;i&gt;Almost everything in javascript is an object.&lt;/i&gt;&#39;&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
That said, lets see how we can create objects.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
There are mainly 2 ways in which you can create objects&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Using the new keyword.&lt;/li&gt;
&lt;li&gt;Literal declaration.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;hr /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;u&gt;Using The New Keyword&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
This is something that programmers from conventional languages are quite used to. The syntax is simple and elegant. You just create a new object and assign it to a variable.&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = new Object();&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Since I have worked extensively in Java before, I can say that this seems quite straightforward. In Java, the Object class is the parent class of all the classes. In the same way, the Object class in javascript is the main class from which all Objects inherit features. And because everything is an object, the fact that javascipt does not have predefined types seems to be a misnomer. There is in fact one master type, and that type is Object. No matter who you are or what you do, remember that the almighty Object is watching you!&lt;/div&gt;
&lt;br /&gt;
If you are not sure of my previous statement, try this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = new Object();
console.log(typeof bingo);
bingo=10;

console.log(typeof bingo);&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As you can see above, we used the typeof operator to determine the type of an object. What you also would have noted is that even though bingo is an object, it was perfectly ok for us to assign a numeric value to it. And when we printed out the type of the variable bingo, we got the type as number. There is a very interesting story of inheritance in javascript that makes this possible. Its something that we will be discussing in absolute details in another article. For now, just be happy with the fact that you dont need to worry about the type of an object before assigning a value to it.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;u&gt;Literal declaration&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
This is perhaps the most common way to declare objects. The reason being, that its so easy that sometimes its baffling that you can even do this.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo2 = {};&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is very easy on the fingers because it saves you a few keystrokes but it is practically equivalent to&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo = new Object();&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If you are not used to this syntax, you better get used to it. Because to be honest, its not such a bad thing. In fact its pretty cool!&lt;/div&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: #6aa84f;&quot;&gt;&lt;u&gt;Declaring Properties On Objects&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
Objects are objects. And objects are meant to store information. However there is is a slight deviation from conventional languages here. When you create objects, you can directly declare new properties on those objects and assign values to them. Unlike Java, where you must have a predefined class where you first define the properties and the methods of the objects that you create, Javascript lets you do things differently. Lets see that with an example.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;var bingo1 = {}; //Create an object of type Object
var bingo2 = {}; //Create another object of type Object
bingo1.shouts = &#39;hey!&#39;;
bingo2.says = &#39;hello&#39;;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Observe carefully. In the above examples, although the type of the objects bingo1 and bingo2 are the same, the language permits you to dynamically declare new properties on each of these objects and those properties dont even need to be same. Although the objects seem to be different because the contain different information, the typeof operator returns the same value for each of these variables.&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:js&quot;&gt;(typeof bingo1==typeof bingo2) //This returns true&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This is something that cannot be done in Java where you can have two objects with a completely different set of properties but still belong to the same class. You dont have the blueprint of a class in Javascript.But that does not mean that you cant have classes in Javascript. It just means that the concept of classes works a bit differently in Javascript.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This makes a short introduction to what is meant by objects in Javascript. That said, in this series, we will explore many more amazing features of the Javascript language such as functions, closures, prototypes and the different ways in which all of these ideas intertwine to create the web as we know it.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Happy Fungramming!&lt;/div&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/04/naked-javascript-object-basics.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-1451013235091438099</guid><pubDate>Sun, 08 Apr 2012 16:54:00 +0000</pubDate><atom:updated>2012-06-05T12:15:24.014+05:30</atom:updated><title>How to clear the javascript console in google chrome or firebug programmatically</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Just a quick tip.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If &amp;nbsp;you are extensively working with the console in the chrome developer tools and the console in Firefox/firebug and you find the need to clear the console many times while you are testing your code, both Chrome and Firefox have a feature that lets you do that programmatically.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;b style=&quot;text-align: justify;&quot;&gt;Chrome&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
clear()&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Firefox&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
console.clear()&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;hr /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
I feel that the Firefox version of the command makes more sense. I have absolutely no idea why the Chrome guys chose to do it differently.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If you are a mouse person, in chrome, you can use the context menu and select Clear Console. If you are using firefox instead, you can use the clear button on top of the firebug console. But I guess you know that already.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
There are a few gocha&#39;s though.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;While you can invoke console.clear() in Firefox from within your own JavaScript to clear the console, you cannot use the clear() function in chrome to clear the console from your own JavaScript.&lt;/li&gt;
&lt;li&gt;Another point to be noted is that in case you are getting an &quot;undefined&quot; message upon typing the clear() function in chrome, you need to disable the option to preserve logs upon navigation for the console. You can do that by right clicking on the console and unticking the &quot;preserve log upon navigation&quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
If you are a fan of typing, chrome also lets you clear the console via a longer api command -&amp;nbsp;console._inspectorCommandLineAPI.clear(). IMO, the shorter version should be sufficient if you want to save typing time.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Happy Programming :)&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Signing Off&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Ryan Sukale&lt;/div&gt;
&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/04/clear-javascript-console-chrome-firebug.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-6777258571920889820</guid><pubDate>Sun, 01 Apr 2012 05:49:00 +0000</pubDate><atom:updated>2012-04-01T11:19:50.230+05:30</atom:updated><title>jdeveloper out of memory heap space error</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Occasionally while working with jDeveloper you may get an OutOfMemory heap space error due to lack of heap space available for the jdeveloper ide itself. The following steps should help.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;
If you have installed jdeveloper on a windows machine you can&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Go to&amp;nbsp;&lt;b&gt;C:\Program Files\&lt;/b&gt;jdeveloper\ide\bin\&lt;br /&gt;&lt;i&gt;&lt;b&gt;NOTE&lt;/b&gt; : The path marked in bold should be your jdeveloper installation directory&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;Edit the file &lt;b&gt;ide.conf&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Set the AddVMOption &amp;nbsp;property to be around -Xmx1024M&lt;/li&gt;
&lt;li&gt;In case after making these changes, jdev doesn&#39;t start up, you probably need to decrease the&amp;nbsp;
AddVMOption &amp;nbsp;to a lower value and restart jdeveloper again&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;br /&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/04/jdeveloper-out-of-memory-heap-space.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-4883550164185523715</guid><pubDate>Thu, 22 Mar 2012 06:57:00 +0000</pubDate><atom:updated>2012-03-22T12:27:22.803+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">java</category><title>Generate Javadoc Through Command Line</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Here&#39;s a quick tip on generating Javadoc HTML files for your Java classes via command line.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
javadoc&amp;nbsp;
&amp;lt;&amp;nbsp;MySourceFileName
&amp;gt;&amp;nbsp;.java -d &amp;lt;Destination Directory&amp;gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You need to make use of the &#39;javadoc&#39; command.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
You can specify multiple files separated by a white space or you can simply specify a directory name containing all your source files.&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
javadoc -sourcepath&amp;nbsp;
&amp;lt;&amp;nbsp;Source Directory
&amp;gt;&amp;nbsp;&amp;nbsp;-d&amp;nbsp;
&amp;lt;&amp;nbsp;Destination Directory&amp;gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Although I havent worked with eclipse, I believe it has an option where you can specify the javadoc command and then you would be able to easily generate documents via some menu option. Just FYI, if you work on windows, the javadoc command is located in the same directory as the java.exe command.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
For more help and options on this command, check out the references&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;References&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://www.mcs.csueastbay.edu/~billard/se/cs3340/ex7/javadoctutorial.html&quot;&gt;http://www.mcs.csueastbay.edu/~billard/se/cs3340/ex7/javadoctutorial.html&lt;/a&gt;&amp;nbsp;- Contains a list of other available options. Its short, sweet and cool.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javadoc.html&quot;&gt;http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javadoc.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.myeclipseide.com/PNphpBB2-viewtopic-t-5191.html&quot;&gt;http://www.myeclipseide.com/PNphpBB2-viewtopic-t-5191.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/03/generate-javadoc-through-command-line.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-9018206052194537874.post-7488910548486875392</guid><pubDate>Wed, 21 Mar 2012 09:09:00 +0000</pubDate><atom:updated>2012-03-21T14:41:12.868+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">ORACLE</category><category domain="http://www.blogger.com/atom/ns#">SNIPPETS</category><title>Get the sql query for a view in Oracle</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Here is a tiny snippet to get the sql query for view in an Oracle database.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:sql&quot;&gt;SELECT text

FROM all_views

WHERE view_name like &#39;%VIEW_NAME%&#39;;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy Programming :)&lt;br /&gt;
Signing Off&lt;br /&gt;
&lt;br /&gt;
Ryan&lt;/div&gt;</description><link>http://mycodefixes.blogspot.com/2012/03/oracle-get-sql-for-view.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item></channel></rss>