Posts

Showing posts from May, 2014

Maya Angelou

Image
The thing I most appreciate about celebrated writer, Maya Angelou , is her bravery.   She spoke and wrote what was on her mind, never mincing words.   In today’s world of empty praise and false positive affirmation, this poet is a fresh ocean breeze in the heat of summer.   I love her poetry and I teach it often along with her memoir, I Know Why The Caged Bird Sings .   Her work connects back to the Harlem Renaissance writers and other African-American artists.   The title of her memoir comes from a poem by Paul Lawrence Dunbar (1872-1906).   However, she is not an artist addressing a particular race; she is a poet for the ages and for all people, a true American treasure. The Washington Post published a piece yesterday by Valerie Strauss that made clear Angelou’s ability to speak the truth.   She was exuberant about the election of President Obama, and he in turn awarded her the Presidential Medal of Freedom in 2011.   However, she often spoke out against the president’s educatio

Checking whether a value is an integer in JavaScript

Integers lead an odd life in JavaScript. In the ECMAScript specification, they only exist conceptually: All numbers are always floating point and integers are ranges of numbers without decimal fractions (for details, consult “ Integers in JavaScript ” in “Speaking JavaScript”). In this blog post, I explain how to check whether a value is an integer. ECMAScript 5 There are many ways in which you could implement this check. At this moment, you may want to take a break and try to write your own solution: a function isInteger(x) that returns true if x is an integer and false , otherwise. Let’s look at a few examples. Checking via the remainder operator One can use the remainder operator ( % ) to express the fact that a number is an integer if the remainder of dividing it by 1 is 0. function isInteger(x) { return x % 1 === 0; } I like this solution, because it is quite self-descriptive. It usually works as expected: > isInteger(17) true > isInteger(17.13

Implementing a raffle in JavaScript

At two recent occasions, there were copies of “Speaking JavaScript” to be raffled off. This blog post describes two ways of drawing winners via JavaScript. There is a list of the attendees’ names If there is a list of the attendees’ names, drawing a winner is relatively easy. In my case, it was a MunichJS meetup and a list of names was online, as a web page . In a browser console, I accessed the DOM to make a random pick: // Once: var nameElements = document.querySelectorAll('span.D_name'); var names = [].map.call(nameElements, function (elem) { // (1) return elem.textContent.trim(); }); // Repeatedly: names[Math.floor(Math.random() * names.length)] // (2) At (1) I invoke map() generically , because the result of querySelectorAll() is not an array and does not have that method. At (2), I use Math.random() and Math.floor() to compute an integer in the range [0, names.length ). You don’t know the attendees’ names If you don’t have a list o

Latinos and Catholicism

Image
Latinos, with a long cultural tradition of Catholicism in their native countries and in the United States, are turning in increasing numbers to Protestantism or simply leaving religious affiliation altogether.   This is disconcerting for Catholic schools and educators because in many dioceses across the country, Latinos are a primary ethnic group in parish classrooms and in the pews on Sundays.   This information comes from the Pew Research Center’s 2013 National Survey of Latinos and Religion , out this month and available online. In the report, 55% of the 35.4 million Latino Americans call themselves Catholic.   Approximately 22% are Protestant and 18% declare themselves unaffiliated with a particular religious group.   According to the report, this means the number of Catholic Latinos has dropped twelve percentage points in the last four years.   “Nearly one-in-four Hispanic adults (24 %) are now former Catholics.” Why is this happening?   The report explains that evangelical or b

Understanding the four layers of JavaScript OOP

Image
JavaScript OOP is baffling: on one hand, there is a simple core, on the other hand, there are some really weird things going on. I’ve been pondering for a long time how to explain it well and I think the best way to do so is via four layers: single objects, prototype chains, constructors, constructor inheritance. The first two layers are JavaScript’s simple OOP core, layers 3 and 4 are where the complications start. The four layers of JavaScript OOP. I’ve explained the four OOP layers in an O’Reilly webcast: Video on YouTube (starts later to avoid duplicated content at the beginning) Slides Full webcast (requires Flash, but the audience’s comments in the chat are interesting) Additional material: “ In defense of JavaScript’s constructors ” explains my – conservative – recommendation of constructors “ JavaScript’s ‘this’: how it works, where it can trip you up ” Chapter “ Objects and Inheritance ” in the online version of “Speaking JavaScript”

We’re doing syntax highlighting wrong

James Fisher mentions two things that are often syntax-highlit wrong: Comments in source code: “The comment is washed out. While the rest of the text exists in black, boldface, and bright colors, the comments fade into the background. […] The implication is obvious: we have collectively decided that the comment is less important than the code.” Insertions and deletions in diff viewers: “[…] a red line is one that was deleted, and a green line is one that was added. […] Our diff viewer, then, tells us that deletions are bad, dangerous, and possibly an error, while insertions are good, safe, and successful.” Consult the blog post for the rationales and several instructive illustrations.

ECMAScript 6’s new array methods

Check out my book (free online): “ Exploring ES6 ”. Updated version of this blog post: chapter “ New Array features ”. This blog post explains what new array methods ECMAScript 6 will bring and how to use them in current browsers. Note: I’m using the terms constructor and class interchangeably. Class methods Array has gained methods of its own. Array.from(arrayLike, mapFunc?, thisArg?) Array.from() ’s basic functionality is to convert two kinds of objects to arrays: Array-like objects , which have a property length and indexed elements. Examples include the results of DOM operations such as document.getElementsByClassName() . Iterable objects , whose contents can be retrieved one element at a time. Arrays are iterable, as are ECMAScript’s new data structures Map and Set . The following code is an example of converting an array-like object to an array: let lis = document.querySelectorAll('ul.fancy li'); Array.from(lis).forEach(function (li) { console.log(n

This Notebook Will Save Your Life

Image
There was a point, a long time ago, when I had no idea what I would do with my life, but I was writing stories.   The urge was insistent, always bubbling underneath whatever was happening.   And little did I know, I was preparing for a future I had yet to imagine.   I was improving my writing skills, and I did not know it. I worked in a parts warehouse for an aerospace firm, a job I got through connections.   We were all college kids trying to earn money for tuition, for a beat-up first car, to try to get out of whatever hell we were trapped in.   The job was easy and paid well.   For four to six hours a day, we pulled parts—washers, nuts, small diodes and circuit boards—which would be packaged into kits and sent to another area of the plant to be assembled into cockpit displays in fighter jets, or radar consoles on a Navy ship.   We had no idea where those tiny bolts and grommets and electrical detritus were headed, or what they would become, but the job involved a lot of counting an