Skip to content

Instantly share code, notes, and snippets.

@itaditya
Last active January 17, 2018 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itaditya/f11adf3a422d86b969116aaf270f2e5c to your computer and use it in GitHub Desktop.
Save itaditya/f11adf3a422d86b969116aaf270f2e5c to your computer and use it in GitHub Desktop.
const barker = (state) => ({
bark: () => console.log('Woof, I am ' + state.name)
})
const driver = (state) => ({
drive: () => state.position = state.position + state.speed
})
const killer = (state) => ({
kill: (enemy) => console.log('I killed ' + enemy)
})
const murderRobotDog = (name) => {
let state = {
name,
speed: 100,
position: 0
}
return Object.assign(
{},
barker(state),
driver(state),
killer(state)
)
}
const bruno = murderRobotDog('bruno')
bruno.bark() // "Woof, I am Bruno"
bruno.kill('Lady Gaga') // "I killed Lady Gaga"
function Greeter(name) {
this.name = name || 'John Doe';
}
Greeter.prototype.hello = function hello() {
return 'Hello, my name is ' + this.name;
}
var george = new Greeter('George');
var proto = {
hello: function hello() {
return 'Hello, my name is ' + this.name;
}
};
var george = Object.create(proto);
george.name = 'George';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment