Skip to content

Instantly share code, notes, and snippets.

@jwalsh
Created August 29, 2012 02:44
Show Gist options
  • Save jwalsh/3506298 to your computer and use it in GitHub Desktop.
Save jwalsh/3506298 to your computer and use it in GitHub Desktop.
function User1( name, age ){
this.name = name;
this.age = age;
}
// Add a new function to the object prototype
User1.prototype.getName = function(){
return this.name;
};
User1.prototype.getAge = function(){
return this.age;
};
var bob = new User1( "Bob", 44 );
console.log("User1: " + bob.getName() + ", Age: " + bob.getAge());
for (var p1 in bob) {
if (bob.hasOwnProperty(p1)) {
console.log(p1);
}
}
// User1: Bob, Age: 44
// name
// age
function User2 (name, age ) {
this.name = name;
this.age = age;
this.getName = function() {
return this.name;
};
this.getAge = function() {
return this.age;
};
}
var bill = new User2( "Bill", 44 );
console.log("User2: " + bill.getName() + ", Age: " + bill.getAge());
for (var p2 in bill) {
if (bill.hasOwnProperty(p2)) {
console.log(p2);
}
}
// User2: Bill, Age: 44
// name
// age
// getName
// getAge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment