Javascript Study Notes - Pseudo-classes and Inheritance

Inheritance in Javascript can be divided into two main categories (non-constructor inheritance and constructor inheritance). Let’s start with the simpler one and get to know constructor-based inheritance. Javascript is a prototype-based language and does not have the concept of classes inherently, so we can only simulate class-based patterns to implement classes and class inheritance. We use constructor functions to simulate the concept of classes. For example, let’s define a Person class. ...

January 6, 2013 · 2 min · Zhiya

A Small Issue Triggered by the 'nettop' Command

I came across a post online mentioning that Mac OSX 10.7 introduced a new command called nettop, which allows you to view current network connections and data flow in real-time. Here is the description of the nettop command: The nettop program displays a list of sockets or routes. The counts for network structures are updated periodically. I decided to give it a try in Terminal. After experimenting with it, I didn’t pay much attention to it. However, about a minute or two after closing Terminal, the fan on my Mac suddenly started spinning wildly. According to iStatMenu, it reached around 5300rpm. Typically, the fan only spins up like this when playing games or watching Flash content, but when I checked the dock, I found that only Chrome, iMessage, and iTunes were open, and no services like Apache were running. Opening Activity Monitor, I discovered that all four CPU cores were maxed out, and two nettop processes were consuming almost all CPU resources. ...

December 19, 2012 · 2 min · Zhiya

browser.js - Browser and Operating System Detection

A JavaScript for browser detection created by me. By importing browser-min.js into the head of your HTML file, it will return a global variable browser, where browser.browser represents the browser, browser.engine represents the browser engine, browser.version represents the browser version, and browser.system represents the operating system. Download the uncompressed version: http://labs.simpleapples.com/browser/browser.js.zip (6.025kb) Download the compressed version: http://labs.simpleapples.com/browser/browser-min.js.zip (1.387kb) Demo: http://labs.simpleapples.com/browser GitHub: http://github.com/simpleapples/browser.js

December 11, 2012 · 1 min · Zhiya

A Small Test About Javascript

I came across a small test called “Do You Really Understand Javascript?” on SAE’s microblog. Although it consists of only a few lines of code, it highlights some easily overlooked issues. Here’s the original image from the microblog: Result of the first code snippet: 1 Javascript does not provide block-level scope, so the variable a inside the if statement’s {} is visible outside. Result of the second code snippet: 1 Here, the function name and the variable name are the same, but ...

December 3, 2012 · 2 min · Zhiya

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