Last active
January 17, 2018 10:03
-
-
Save itaditya/f11adf3a422d86b969116aaf270f2e5c to your computer and use it in GitHub Desktop.
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
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" |
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
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'); |
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
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