Exploring the 'new' Operator in Javascript

In Javascript, there’s an interesting operator called new. In JavaScript: The Good Parts, new is listed as a not recommended operator. Let’s explore the usage of new. Consider the following code: function test() { var foo1 = new function () { this.i = 1; } var foo2 = function () { this.i = 2; } M.dis(foo1.i); // 1 M.dis(foo2.i); // undefined M.dis(this.i); // undefined foo2(); M.dis(foo2.i); // undefined M.dis(this.i); // 2 M.dis(foo1.prototype); // undefined M.dis(foo2.prototype); // [object Object] } In the code, M.dis() is equivalent to document.writeln(). The above code clearly illustrates the difference between using and not using the new operator. Below are my understandings based on the results of using and not using the new operator. ...

December 2, 2012 · 3 min · Zhiya

MagicSearch

This evening, I came across an OSX application called Alfred, which is a tool similar to Spotlight but much more powerful. Not only can you search local programs with it, but by adding “>” before the search content, you can input commands, directly enter calculations, and customize search engines using a format like “google Hello World”. I suddenly wondered if this could be implemented using JavaScript. So, I first implemented the multi-search engine integration feature. I didn’t want to use the old dropdown menu style; instead, I wanted to use an autocomplete format, which took quite a bit of effort, but I managed to get the functionality working. For example, typing “douban The Ordinary World” allows you to search “The Ordinary World” on Douban, and typing “weibo frontend” lets you search for “frontend”-related content on Weibo. You can also type “t” + Enter, then input a keyword to search on Taobao. Currently, I’ve only added a few simple search tools (because my friend AlisterTT, who helped design the interface, only gave me these icons = =!): Weibo, Google, Douban, Taobao, and Amazon. I’ve given it a catchy name, MagicSearch, and it would be even better if it could be developed for mobile clients in the future, as it should be quite useful. There are still many things and details to be modified. Also, the calculator function hasn’t been added yet, and my computer is running out of battery, so I’ll have to wait until tomorrow. Here’s a screenshot for now: ...

December 1, 2012 · 2 min · Zhiya

Loading SWF Files in Flash and Playing Frame by Frame

In Flash, importing and playing SWF files can be easily done using the loadMovie() function in ActionScript. However, playing them frame by frame is a bit more complex. When playing frame by frame, you either need to set a play time for each frame or manually control it with buttons. Here, we’ll take an example of playing frame by frame based on time, and explain how to control it using ActionScript. ...

November 22, 2012 · 1 min · Zhiya

Compass WebApp

A Compass WebApp has been created using JavaScript. The app listens to the DeviceOrientation event, where the device generates three values: alpha, beta, and gamma when it changes orientation. The alpha value represents the current angle. window.addEventListener('deviceorientation', function(event) { target.style.transform = 'rotate(' + event.alpha + ')'; }, false); The alpha value is 0° at true north and increases clockwise to 360°, corresponding to the rotation angle of the compass needle. The CSS3 transform property is used to rotate the needle. ...

November 14, 2012 · 2 min · Zhiya

Decrypting Thunder Links

You often come across Thunder links starting with thunder:// on the internet, which cannot be downloaded by other software. In fact, Thunder links are the result of base64 encryption. By decrypting them, you can retrieve the original link address. Let’s take the link to the Baidu logo image as an example. The original link is: http://www.baidu.com/img/baidu_sylogo1.gif Thunder adds “AA” at the beginning and “ZZ” at the end of the original link, forming a new string: ...

November 12, 2012 · 1 min · Zhiya