Mutators in JavaScript

I was surprised today by an unexpected behavior of JavaScript. In most computing languages the idea of references is often confusing to new programmers, but for some reason this is not emphasized as much in JavaScript. However it is not a topic that can be safely ignored.

var arr = [1, 2, 3];

function foo(arr) {
    arr.shift();   // modifies the array "arr" refers to
    arr.push("a");
    arr[3] = "b";
}
foo(arr);

console.log(arr); // [2, 3, "a", "b"]

A Billion Octothorps

I can tell you from experience that there can be a lot of confusion when Americans speak to Brits about this symbol here → #

You can't use the word "pound," because they've already got a different symbol described by that word: £

The word info website lists some other possible names:

... crosshatch, hash, numeral sign and number sign;

In order to be equally unfair to all, I propose we start using the term "octothorp" instead. From Robert Bringhurst, The Elements of Typographic Style; 2nd edition:

Otherwise known as the numeral sign. It has also been used as a symbol for the pound avoirdupois, but this usage is now archaic. In cartography, it is also a symbol for village: eight fields around a central square, and this is the source of its name. Octothorp means eight fields.

And while I'm on the subject, there are the occasional Britcentrics who will correct you if you try to use the term "billion" to mean this number: 1,000,000,000. Apparently, in the past, Britain used what's called the "long scale" numbering system in which:

  • 1 billion = 1 million millions, 1,000,000,000,000
  • 1 trillion = 1 million billions, 1,000,000,000,000,000,000
  • and so on.

For the life of me I can't imagine why the UK would want to use a numbering system where each unit becomes exponentially larger than the same unit used by the US. Even the British government realized that was a bad idea and switched to the "short scale" numbering system in 1974 (presumably that was about the time their budgets starting regularly getting up into those figures). Today all UK government and national media (including the BBC) use the same system as the US: the short scale.


Properties of Primitives

I was tripped up momentarily today, swept away by the flexibility of JavaScript, where you can add and modify properties of anything whenever you feel the whim to do so. Well, it turns out that there are a few exceptions to that rule: you can't add properties to primitive data types. JavaScript has the following primitives:

  • Number
  • String
  • Boolean
  • Undefined
  • Null

And the example that shows a primitive resisting my attempt to stick a property onto it:

var s1 = "quick brown fox";
s1.lang = "en";
alert(s1.lang); // undefined

But there is a solution if you want to do this sort of thing: you must wrap your primitive data up in an object-instance cloak.

var s2 = new String("quick brown fox");
s2.lang = "en";
alert(s2.lang); // en

A few more examples of primitive wrappers (you cannot construct a new Undefined or Null):

  • var value = new Number(3.14);
  • var value = new String("hello");
  • var value = new Boolean(false);

Interestingly these wrappers affect the result returned by typeof, causing them to all become "objects" whose constructor is equal to either Number, String or Boolean. You do some coersion with string to promote it to an object and then demote it back to a primative, like so:

// start with a primitive
var s1 = "hello";
alert(typeof s1); // string
alert(s1.constructor == String); // false

// promote to an object
var s2 = new String(s1);
alert(typeof s2); // object
alert(s2.constructor == String); // true

// demote back to a primitive
var s3 = ""+s2;
alert(typeof s3); // string
alert(s3.constructor == String); // false

contact

tags

archive

more blogs