Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Created March 3, 2017 10:18
Show Gist options
  • Save mikepenzin/26e75ed6fd0688533c7c78e74ca7da03 to your computer and use it in GitHub Desktop.
Save mikepenzin/26e75ed6fd0688533c7c78e74ca7da03 to your computer and use it in GitHub Desktop.
Prototyping Patterns
// 1. Init method
var Person = {
init: function(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
return this;
},
full_name: function(firstName, lastName){
return this.firstName + " " + this.lastName;
}
}
var mike = Object.create(Person);
mike.init("Mike", "Penzin");
console.log(mike.full_name()); // Output: Mike Penzin
// ------------
// 2. Object creation method
var Person = {
full_name: function(firstName, lastName){
return this.firstName + " " + this.lastName;
}
}
var luc = Object.create(Person, {
firstName: {
value: "Luc"
},
lastName: {
value: "Besson"
}
});
var mike = Object.create(Person, {
firstName: {
value: "Mike"
},
lastName: {
value: "Penzin"
}
});
console.log(luc.full_name()); // Output: Luc Besson
console.log(mike.full_name()); // Output: Mike Penzin
// ------------
// 3. Creation function method
var Person = {
full_name: function(firstName, lastName){
return this.firstName + " " + this.lastName;
}
}
function PersonFactory(firstName, lastName){
var person = Object.create(Person);
person.firstName = firstName;
person.lastName = lastName;
return person;
}
var luc = PersonFactory("Luc", "Besson");
var mike = PersonFactory("Mike", "Penzin");
console.log(luc.full_name()); // Output: Luc Besson
console.log(mike.full_name()); // Output: Mike Penzin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment