Created
November 18, 2011 22:37
-
-
Save EddieDev/1377994 to your computer and use it in GitHub Desktop.
Javascript Code Challenge and Implementation. http://eblundell.com/javascript-challenge
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
Array.prototype.joinProperty = function(delim,method) { | |
var output = ''; | |
for(var i=0; i < this.length; i++){ | |
if(this[i].hasOwnProperty(method)){ | |
output = output.concat(this[i][method],delim); | |
}else if(eval("typeof this[i]."+method+" != 'undefined'")){ | |
output = output.concat(eval('this[i].'+method+'()'),delim); | |
} | |
} | |
return output.slice(0,output.length-delim.length); | |
} | |
x = {item:'coffee'} | |
y = {item:'milk'} | |
z = {item:'tea'} | |
var myArr = [x,y,z]; | |
console.log(myArr.joinProperty(',','item')); | |
/** | |
* @constructor | |
*/ | |
function testObject(message){ | |
this.message = message; | |
} | |
testObject.prototype.getMessage = function() { | |
return this.message; | |
}; | |
a = new testObject('paper'); | |
b = new testObject('plastic'); | |
c = new testObject('metal'); | |
var secondArr = [a,b,c]; | |
console.log(secondArr.joinProperty(',','getMessage')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment