Posts

Showing posts from September, 2013

Tips for using window in JavaScript

In web browsers, window refers to an object that contains the global variables. This blog post explains how it works and when to use it. The global object and window The ECMAScript specification uses the internal data structure environment to store variables. The language has the somewhat unusual feature of making the environment for global variables accessible as an object, the so-called global object . The global object can be used to create, read and change global variables. In global scope, this points to it: > var foo = 'hello'; > this.foo // read global variable 'hello' > this.bar = 'world'; // create global variable > bar 'world' In web browsers, the global variable window also points to the global object. JavaScript creator Brendan Eich considers the global object one of his “ biggest regrets ”. Cross-platform considerations Node.js does not have a global variable window . Therefore, the only safe cross-p

Unicode and JavaScript

Update 2013-09-29: New sections 4.1 (“Matching any code unit”) and 4.2 (“Libraries”). This blog post is a brief introduction to Unicode and how it is handled in JavaScript. Unicode History Unicode was started in 1987, by Joe Becker (Xerox), Lee Collins (Apple) and Mark Davis (Apple). The idea was to create a universal character set, as there were many incompatible standards for encoding plain text at that time: numerous variations of 8 bit ASCII, Big Five (Traditional Chinese), GB 2312 (Simplified Chinese), etc. Before Unicode, no standard for multi-lingual plain text existed, but there were rich text systems (such as Apple’s WorldScript) that allowed one to combine multiple encodings. The first Unicode draft proposal was published in 1988. Work continued afterwards and the working group expanded. The Unicode Consortium was incorporated on January 3, 1991: The Unicode Consortium is a non-profit corporation devoted to developing, maintaining, and promoting software internationaliz

The ECMAScript Internationalization API

The ECMAScript Internationalization API is a standard JavaScript API that helps with tasks related to internationalization: collation, number formatting, date and time formatting. This blog post gives a brief overview and points to more reading material. The ECMAScript Internationalization API, edition 1 The first edition of the API provides the following services: Collation. supports two scenarios: sorting a set of strings and searching within a set of strings. Collation is parameterized by locale and aware of Unicode. Number formatting. Parameters include: Style of formatting: decimal, currency (which one and how to refer to it, is determined by other parameters), percent. Locale (directly specified or best fit, searched for via a matcher object) Numbering system (Western digits, Arabic digits, Thai digits, etc.) Precision: number of integer digits, fraction digits, significant

The Mansion of Happiness

Image
Jill Lepore’s The Mansion of Happiness:   A History of Life and Death (Vintage Books, $16) should really be called “The History of Everything.”   She moves digression to the realm of art.   These essays, many appearing first on the pages of The New Yorker , provide an interesting and exhilarating journey for the reader who dares to follow Lepore’s blazing trail of prose through the meanderings of human history and experience. “This book is a history of ideas about life and death from before the cradle to beyond the grave,” Lepore writes in her Preface.   We should forgive the cliché.   It may be history, but the book also has a free-associative quality that keeps surprising the reader at every turn.   There are intricate connections and themes that link figures and events, and Lepore obviously finds joy in her work because it is present on every page.   Her prose is often humorous and barbed as she explores her subjects.   She is a story teller, and therefore, the best kind of teache

OS X: kill all Google Chrome tabs from the shell

If there are a lot of tabs open in Google Chrome, it tends to become slow. Killing tabs by hand, via the Task Manager helps, but is tedious. The following bash script (by Sindre Sorhus ) lets you conveniently kill all open tabs from the shell (OS X only): #!/bin/bash ps ux | \ grep '[C]hrome Helper --type=renderer' | \ grep -v extension-process | \ tr -s ' ' | \ cut -d ' ' -f2 | \ xargs kill # [C] explained: http://www.commandlinefu.com/commands/view/402/exclude-grep-from-your-grepped-output-of-ps-alias-included-in-description That is, all tabs are still open, but they don’t display any content. You can reload any tab to get the content back. I’d love Chrome to have a mode where all background tabs are stopped completely and only the frontmost tab is active. Kind of like processes are managed on iOS.

Speaking Truth To Power

Image
Noa Rosinplotz from Facebook and other websites The following is a letter written a while back by a sixth grader to Secretary of Education Arne Duncan. It has appeared in many places since its first publication. I got it from Diane Ravitch's blog . The writer is a student in Washington D.C. named Noa Rosinplotz . Washington Post reporter Jay Mathews was able to verify that Noa is the daughter of Slate editor-in-chief, David Plotz , and former Post reporter Hanna Rosin . Although her parents read her letter before she published it, they did not write it for her. Noa is a gifted student. She nails the problem with standardized testing, No Child Left Behind, and Race To The Top. For that reason, this piece must be shared. Here is Noa Rosinplotz's letter: Dear Mr. Duncan, I’m writing you because I got my DC CAS results in the mail. See, I thought you might want to know what they were. I certainly don’t. I mean, the first thing I noticed in that packet was the paper. It’s fanc

Data in prototype properties

Update 2013-09-14: New sections 1.2, 2 and 3. This blog post explains when you should and should not put data in prototype properties. Avoid: prototype properties with initial values for instance properties Prototypes contain properties that are shared by several objects. As such, they work well for methods. Additionally, with the technique shown below, you can also use them to provide initial values for instance properties. I’ll later explain why that is not recommended. A constructor usually sets instance properties to initial values. If one such value is a default then you don’t need to create an instance property. You only need a prototype property with the same name whose value is the default. For example: /** * Anti-pattern: don’t do this * * @param data an array with names */ function Names(data) { if (data) { // There is a parameter // => create instance property this.data = data; } } Na

Change Is Gonna Come

Image
The Conversion of St. Paul “It’s been too hard living but I’m afraid to die Cause I don’t know what’s up there beyond the sky It’s been a long, a long time coming But I know a change gonna come, oh yes it will.”                                                                                     -Sam Cooke What makes us change our minds?   What makes us change our lives?   Henry David Thoreau writes that, “Things do not change; we change.”   Yet change can be the thing we fear most.   To change, we must first let go of what was, of the way things used to be, of the people we once were.   We must free-fall into the unmapped chasm of the future.   We must leap and embrace the fall. Saint Paul, the man after whom I am named, had a major conversion experience in his life.   He was Saul, a Pharisee, an observant Jew who studied Mosaic Law and considered himself a zealot for the Chosen People.   He hated the reformers who took their cues from Jesus Christ, and actively persecuted them with m