Skip to content

Instantly share code, notes, and snippets.

@ckung
Last active December 24, 2015 14:38
Show Gist options
  • Save ckung/6813685 to your computer and use it in GitHub Desktop.
Save ckung/6813685 to your computer and use it in GitHub Desktop.
Object.prototype.explode = function() {
return 'KABOOM!';
};
var addAll = function(things, value) {
for (var i in things) {
things[i].push(value);
}
return things;
}
var things = {
'first' : [],
'second' : []
};
console.log(addAll(things, 1));
// Expected: { first: [ 1 ], second: [ 1 ] }
// Actual: error! function has no method 'push'
var safeAddAll = function(things, value) {
for (var i in things) {
if (this.hasOwnProperty(i)) {
things[i].push(value);
}
}
return things;
}
console.log(safeAddAll(things, 1));
// { first: [ 1 ], second: [ 1 ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment