A Pen by Keegan Brown on CodePen.
Created
January 14, 2014 17:15
-
-
Save keeganbrown/8422009 to your computer and use it in GitHub Desktop.
A Pen by Keegan Brown.
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
<div id="output"></div> |
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
//create an object | |
var friends = {}; | |
var Friend = function ( config ) { | |
this.firstName = config.firstName; | |
this.lastName = config.lastName; | |
this.number = config.number; | |
this.address = config.address; | |
this.email = config.email; | |
this.getMyFullName = function () { | |
logger( this.firstName + " " + this.lastName ); | |
} | |
this.getMyFullName(); | |
} | |
//add people to it | |
friends.bill = new Friend({ //observe this declaration as bill is an object of friends | |
firstName: "Bill", | |
lastName: "Gates", | |
number: "(206) 555-5555", | |
address: ['One Microsoft Way','Redmond','WA','98052'] | |
}); | |
//add another | |
friends.steve = new Friend({ | |
firstName: "Steve", | |
lastName: "Jobs", | |
number: "(408) 555-5555", | |
address: ['1 Infinite Loop','Cupertino','CA','95014'] | |
}); | |
//log out all contents of the object | |
function list (obj) { | |
for(var prop in obj) { | |
logger(prop); | |
} | |
}; | |
//search for "steve" | |
function search (name) { | |
for(var prop in friends) { | |
if(friends[prop].firstName === name) { | |
logger(friends[prop]); | |
return friends[prop]; | |
} | |
} | |
}; | |
function add (firstName, lastName, phoneNumber, email){ | |
friends[ firstName.toLowerCase() ] = new Friend({ | |
firstName: firstName, | |
lastName: lastName, | |
number: phoneNumber, | |
email: email | |
}); | |
} | |
function logger () { | |
for ( var i in arguments ) { | |
document.getElementById("output").innerHTML += "<br>"+ arguments[i]; | |
console.log(arguments[i]); | |
} | |
} | |
add("Cyril", "Celestine", "5042282838", "[email protected]"); | |
list(friends); | |
var tmp = search("Cyril"); | |
console.log(friends); | |
tmp.getMyFullName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment