Skip to content

Instantly share code, notes, and snippets.

@akrueger
Last active July 27, 2016 23:51
Show Gist options
  • Save akrueger/8c3bb84fef43600d3d436fb511535723 to your computer and use it in GitHub Desktop.
Save akrueger/8c3bb84fef43600d3d436fb511535723 to your computer and use it in GitHub Desktop.
JavaScript Object Composition techniques
/***** SETUP *****/
// Constructor
function Car(model) {
this.model = model
}
// Factory
function Truck(model) {
return {
model
}
}
// Object literal
const Moto = {}
/***** Assign and Augment Prototypes *****/
// Constructor
const mazda = new Car('miata')
Car.prototype.getModel = function() {
console.log(this.model)
}
// Factory
const chevy = Truck('silverado')
Truck.prototype.getModel = function() {
console.log(this.model)
}
Object.setPrototypeOf(chevy, Truck.prototype)
// Object.create
const triumph = Object.create(Moto)
triumph.model = 'bonnevile'
Moto.getModel = function() {
console.log(this.model)
}
/***** Functional Concatenative -- No prototype *****/
function Plane(spec) {
const
{model} = spec,
getModel = function() {
console.log(model)
}
return Object.freeze({
getModel
})
}
const airbus = Plane({
model: 'a320'
})
/***** Example *****/
function createSnake(spec) {
const
{name, age, hostilityLevel} = spec,
species = 'Snake',
thermoregulation = 'ectothermic',
venemous = true,
getName = function() {
return name
}
bite = function() {
return 'OUCH!'
},
pet = function() {
if(hostilityLevel > 7) {
return `You've been bitten by ${name} the ${species}`
}
else {
return 'Purrrr'
}
},
isColdBlooded = function() {
if(thermoregulation === 'ectothermic') {
return true
}
}
return Object.freeze({
getName,
bite,
venemous,
pet,
isColdBlooded
})
}
function createElephant(spec) {
const
{name, age, hostilityLevel} = spec,
species = 'Elephant',
thermoregulation = 'endothermic',
hair = true,
getName = function() {
return name
}
eatPeanut = function() {
return 'nom nom nom'
},
pet = function() {
if(hostilityLevel > 7) {
return `You've been bitten by ${name} the ${species}`
}
else {
return 'Purrrr'
}
}
return Object.freeze({
getName,
hair,
thermoregulation,
pet
})
}
function createFish(spec) {
const
{name, age, hostilityLevel} = spec,
species = 'Fish',
thermoregulation = 'ectothermic',
swims = true,
getName = function() {
return name
}
swim = function() {
return `Swim, ${name} swim!`
},
pet = function() {
if(hostilityLevel > 7) {
return `You've been bitten by ${name} the ${species}`
}
else {
return 'Purrrr'
}
}
return Object.freeze({
getName,
swims,
pet,
swim
})
}
function createBird(spec) {
const
{name, age, hostilityLevel} = spec,
species = 'Bird',
thermoregulation = 'endothermic',
laysEgg = true,
getName = function() {
return name
},
fly = function() {
return 'Let the eagle SOOOOAAAAR!!!'
},
pet = function() {
if(hostilityLevel > 7) {
return `You've been bitten by ${name} the ${species}`
}
else {
return 'Purrrr'
}
}
return Object.freeze({
getName,
laysEgg,
pet
})
}
function createPlatypus(spec) {
const
{name, age, hostilityLevel} = spec,
species = 'Platypus',
{venemous} = createSnake(spec),
{thermoregulation, hair} = createElephant(spec),
{swims, swim} = createFish(spec),
{laysEgg} = createBird(spec),
getName = function() {
return name
},
pet = function() {
if(hostilityLevel > 7) {
return `You've been bitten by ${name} the ${species}`
}
else {
return 'Purrrr'
}
},
isMature = function() {
return age > 2 ? true : false
}
return Object.freeze({
getName,
venemous,
hair,
swims,
swim,
laysEgg,
pet,
isMature
})
}
/*
const perry = createPlatypus({
name: 'Perry',
age: 4,
hostilityLevel: 8
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment