Object Keys in Javascript

Let’s take a look at a piece of code: <div id="e1"></div> <div id="e2"></div> <script> var c1 = document.getElementById("e1"); var c2 = document.getElementById("e2"); var obj = {}; obj[c1] = 1; obj[c2] = 2; console.log(obj); </script> What will this code output? The answer is: Object {[object HTMLDivElement]: 2} Why does it output only one element when we assigned two elements to obj? Could it be that c1 and c2 point to the same element? First, c1 and c2 are two different DOM nodes, and by outputting c1 === c2, you’ll find the result is false. However, the result of obj[c1] === obj[c2] is true. From the above results, we can initially deduce that when c1 and c2 are used as object keys, their values might be the same. An object’s key can only be a string, so when the DOM element c1 is used as a key, it is converted to a string. Since both c1 and c2 are div elements, they are converted to the same string value, and c2 as a key will overwrite c1. This results in only one element being present. If c2 is changed to a different type of DOM node, it will be converted to a different string value. For example, if c2 is changed to a ul, using the same JavaScript code, the output will be: ...

January 8, 2014 · 2 min · Zhiya

Do Not Name LiteIDE Project Folder 'go'

When writing Go programs in LiteIDE, using Ctrl+R to compile and run, the Console outputs the same content every time, even if the code has changed. It still shows the content of the initial code, meaning the new code is not being compiled at all. Initially, I suspected it was an issue with LiteIDE, so I restarted LiteIDE and even the system, but the problem persisted. Creating a new project in a different location allowed for normal compilation and execution, but when I copied the source files created elsewhere back to the original project location, the issue of not being able to compile reappeared. ...

October 30, 2013 · 2 min · Zhiya

Cracking the Google Doodle Game

Today seems to be Google’s fifteenth anniversary, and as usual, they have a JS game on their homepage. The game involves hitting a star with a stick (ten attempts), and if you time it right, candies will drop. After various attempts, my highest score was 173. I initially thought this was a high score, but after checking on Weibo, I found that quite a few people scored over 180, and there’s an Easter egg for scores above 180. I couldn’t let this go, so I kept trying but couldn’t surpass 173. Out of options, I decided to approach it programmatically. Since it’s a JS game, theoretically, by modifying the number of attempts, I could increase the score. I went to the Google homepage to find the JS file and easily discovered a file at https://www.google.com.hk/logos/2013/bday13/bday13.js. Searching for the number 10, I found many results. Using intuition, I suspected that the assignment bd=10 was the key, so I changed it. Refreshed, and success! Below is the score after changing it to 99 attempts: ...

September 27, 2013 · 1 min · Zhiya

Issue of Not Finding Base Class BitmapAsset After Upgrading to Flex SDK 4.9.1

A project compiled using the Flex SDK encountered the following error after upgrading the SDK to 4.9.1: errors: 1017: The definition of base class BitmapAsset was not found. After some Googling, I found that others have encountered this issue as well. See more details here: http://mail-archives.apache.org/mod_mbox/flex-dev/201303.mbox/%3CCD596C86.4E4D3%[email protected]%3E The core.swc in Flex SDK 4.9 is about half the size of the core.swc in 4.8, which suggests that some base classes have been moved out of core.swc, leading to the BitmapAsset not being found. ...

August 27, 2013 · 1 min · Zhiya

Setting Up an Actionscript Development Environment on Linux

Actionscript development is typically done using Flash Pro and Flash Builder, but neither of these software has a Linux version. On Linux, we can manually set up an Actionscript development environment. The Actionscript development environment mainly consists of three components: the IDE, SDK, and Flash Player Debugger. IDE Adobe provides Flash Builder specifically for Actionscript development, which is based on Eclipse. Since Eclipse offers a Linux version, we can achieve Flash Builder functionality on Linux by installing the Actionscript plugin on Eclipse. First, download the Eclipse IDE from the Eclipse official website. The latest version is 4.3. The installation process is not detailed here. Eclipse requires a Java environment to run. Users of Linux distributions without pre-installed Java can visit the Oracle website to download the appropriate installation package. After installing Eclipse, you need to install the Flash Builder plugin. Since there is no official plugin for Linux, we use an open-source Flash Builder 4.5 Linux plugin called fb4linux. The project can be found at: http://code.google.com/p/fb4linux/. Visit this link to download the FB4.5ForLinuxaa and FB4.5ForLinuxab packages. Once downloaded, use the command cat FB45ForLinux* >FB45ForLinux.tar.bz2 to combine the two packages into a single compressed file and then extract it. Next, install the FB45ForLinux plugin in Eclipse. In Eclipse, go to Help->Install New Software, click Add, and in the pop-up window, select Local. Navigate to the directory where FB45ForLinux.tar.bz2 was extracted, and click OK. If no installable plugins appear in the list, uncheck the Group items by category option to display the available software. Select all available items and click Next to install. Once the plugin installation is complete, the IDE configuration is done. Next is the SDK installation. ...

August 4, 2013 · 3 min · Zhiya