Created
March 12, 2015 06:08
-
-
Save macinnir/9ddf60d65b635842aaf7 to your computer and use it in GitHub Desktop.
Extending Object and Array and the unexpected results in looping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Object/Array native objects being extended via prototype produces unexpected results when | |
* when looping through objects/arrays respectively. | |
* @author Rob MacInnis | |
* @see http://stackoverflow.com/questions/10695506/adding-function-to-object-prototype-causes-function-to-show-up-in-all-for-x-in | |
*/ | |
require('colors'); | |
// Extending a native JS object | |
Array.prototype.foo = function(i) { | |
return i + 1; | |
}; | |
Array.prototype.bar = 'bar'; | |
// Doesn't show up | |
Array.baz = 'baz'; | |
var a = new Array(); | |
// console.log(a.foo(1)); // 2 | |
// The `foo` function shows up in the Array.__proto__ | |
// affecting the usage of arrays henceforth | |
var a1 = [1,2,3]; | |
console.log('-----------------'.blue); | |
console.log('Test 1.1: Array looping shows functions added via prototype'.blue); | |
console.log('-----------------'.blue); | |
for(var i in a1) { | |
console.log('Key'.green, i, 'Val'.green, a1[i]); // 0, 1, 2, 3, foo <-- shouldn't be there | |
} | |
console.log('-----------------'); | |
console.log('Test 1.2: Using current Array hasOwnProperty to filter results'); | |
console.log('-----------------'); | |
for(var i in a1) { | |
if(a1.hasOwnProperty(i)) { | |
console.log('Key', i, 'Val', a1[i]); | |
} | |
} | |
console.log('-----------------'.blue); | |
console.log('Test 2.1: Object looping contains prototype function'.blue); | |
console.log('-----------------'.blue); | |
Object.prototype.bar = function() { | |
return 'bar'; | |
}; | |
var b = new Object(); | |
var b1 = { baz: 'quux' }; | |
for(var i in b1) { | |
console.log('Key'.green, i, 'Val'.green, b1[i]); // bar | |
} | |
console.log('-----------------'.blue); | |
console.log('Test 2.2: Using current object hasOwnProperty to filter results'.blue); | |
console.log('-----------------'.blue); | |
for(var i in b1) { | |
if(b1.hasOwnProperty(i)) { | |
console.log('Key'.green, i, 'Val'.green, b1[i]); | |
} | |
// if(Object.prototype.hasOwnProperty(i)) { | |
// console.log('Key', i, 'Val', b1[i]); | |
// } | |
} | |
/** | |
* ----------------- | |
* Test 1.1: Array looping shows functions added via prototype | |
* ----------------- | |
* Key 0 Val 1 | |
* Key 1 Val 2 | |
* Key 2 Val 3 | |
* Key foo Val function (i) { | |
* return i + 1; | |
* } | |
* Key bar Val bar | |
* ----------------- | |
* Test 1.2: Using current Array hasOwnProperty to filter results | |
* ----------------- | |
* Key 0 Val 1 | |
* Key 1 Val 2 | |
* Key 2 Val 3 | |
* ----------------- | |
* Test 2.1: Object looping contains prototype function | |
* ----------------- | |
* Key baz Val quux | |
* Key bar Val function () { | |
* return 'bar'; | |
* } | |
* ----------------- | |
* Test 2.2: Using current object hasOwnProperty to filter results | |
* ----------------- | |
* Key baz Val quux | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment