Skip to content

Instantly share code, notes, and snippets.

@EddieDev
Created November 18, 2011 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EddieDev/1377994 to your computer and use it in GitHub Desktop.
Save EddieDev/1377994 to your computer and use it in GitHub Desktop.
Javascript Code Challenge and Implementation. http://eblundell.com/javascript-challenge
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