purescript/purescript-foreign — pull request #18 by davidchambers (2014-11-16 00:46:47+00:00)
Title: add ES3 fallbacks for Array.isArray and Object.keys

The [MDN polyfill][1] is unappealing:

```javascript
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;

return function(obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}

var result = [], prop, i;

for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}

if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
```

I opted for a simpler version which doesn't make allowances for a bug in old versions of Internet Explorer. I'm happy to switch to the foolproof (but ugly!) fallback if desired.

Also, this is interesting:

> In ES5, if the argument to this method is not an object (a primitive), then it will cause a `TypeError`. In ES6, a non-object argument will be coerced to an object.
>
> ```javascript
> > Object.keys("foo")
> TypeError: "foo" is not an object // ES5 code
>
> > Object.keys("foo")
> ["0", "1", "2"]
> ```

What's the desired behaviour in our case? We should avoid `Object.keys` entirely to avoid generating code with environment-dependent semantics.

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys