Attaboy Media

Month

January 2011

7 posts

“The catch is that when it comes to fat and calories, there is very little to distinguish between boneless, skinless chicken breast and boneless, skinless thighs. According to the Department of Agriculture, 100 grams of the former contains 0.56 grams of saturated fat and 114 calories, and the latter 1 gram of saturated fat and 119 calories. Dark chicken meat is also nutrient rich, containing higher levels of iron, zinc, riboflavin, thiamine, and vitamins B6 and B12 than white meat.” —from “Americans overwhelmingly prefer white chicken meat. What happens to the dark parts?” at Slate Magazine
Jan 26, 20111 note
Jan 26, 2011
For-in loops ignore properties named 'toString' in IE

@matasar and I discovered a really neat “feature” of Internet Explorer: if you enumerate an object using a for…in loop, IE will ignore any property named toString. This bit us because we were using such a loop to add methods to an object’s prototype using code like this:

var someNewMethods = {
  someOtherMethod: function() {
  …
  },
  toString: function() { // overrride native toString
    return 'I am a foo.';
  }
}

for (var p in someNewMethods) {
  ExistingObjectType.prototype[p] = someNewMethods[p];
}

In sane browsers like Firefox, Safari and Chrome, this will add someOtherMethod and override the toString method of the ExistingObjectType’s prototype. In IE, toString is ignored in the loop, so it’s left unmodified. (And the native toString method for any object is always the ever-helpful string "[Object object]".)

It turns out this is a known bug in IE. Essentially, for…in loops ignore any properties that would otherwise be inherited from the Object type, including toString, valueOf and others.

The solution, sadly, appears either to be to avoid ever using properties with those names (at least if you expect to need to enumerate them), or to treat them as a special-case, with some code like this:

var thoseOtherProps = [
  'constructor',
  'toString',
  'valueOf',
  'toLocaleString',
  'isPrototypeOf',
  'propertyIsEnumerable',
  'hasOwnProperty'
];

thoseOtherProps.forEach(function(p) {
  if (someNewMethods.hasOwnProperty(p)) {
    ExistingObjectType.prototype[p] = someNewMethods[p];
  }
});

Blech. (And of course, IE also doesn’t support forEach natively, so this code assumes that you’ve added such a method to the Array prototype.)

Jan 25, 2011
HTML5 is dead! Long live HTML! → blog.whatwg.org
Jan 20, 20111 note
Jan 16, 20111 note
Jan 9, 2011
The Decemberists new album, "The King Is Dead", streaming at NPR → npr.org
Jan 4, 2011
Next page →
2012 2013
  • January 3
  • February
  • March 2
  • April
  • May
  • June
  • July
  • August
  • September
  • October
  • November
  • December
2011 2012 2013
  • January 28
  • February 7
  • March 6
  • April 4
  • May 4
  • June 3
  • July 4
  • August 3
  • September 5
  • October 3
  • November
  • December
2010 2011 2012
  • January 7
  • February 5
  • March 7
  • April 10
  • May 6
  • June 3
  • July 6
  • August 13
  • September 14
  • October 11
  • November 15
  • December 20
2009 2010 2011
  • January 23
  • February 20
  • March 25
  • April 28
  • May 25
  • June 21
  • July 13
  • August 9
  • September 1
  • October 9
  • November 14
  • December 5
2008 2009 2010
  • January 13
  • February 13
  • March 17
  • April 16
  • May 18
  • June 7
  • July 27
  • August 8
  • September 18
  • October 21
  • November 14
  • December 21
2007 2008 2009
  • January 4
  • February 3
  • March 13
  • April 16
  • May 4
  • June 9
  • July 4
  • August 15
  • September 9
  • October 11
  • November 11
  • December 8
2007 2008
  • January
  • February
  • March
  • April
  • May
  • June
  • July
  • August
  • September
  • October
  • November 13
  • December 8