Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created May 31, 2016 16:16
Show Gist options
  • Save octosteve/dacc0b1e14782a6a73338ca7c753eb41 to your computer and use it in GitHub Desktop.
Save octosteve/dacc0b1e14782a6a73338ca7c753eb41 to your computer and use it in GitHub Desktop.
var Person = (function() {
'use strict';
// using named expressions for no special reason
var sayHi = function(){
console.log(`Hi, it's ${this.name}`);
}
var addFriend = function (friendName) {
this.friends.push(friendName)
};
var greetFriends = function () {
this.friends.forEach(greetFriend, this)
};
var greetFriend = function (friend) {
console.log(`${this.name} says hi to ${friend}`);
};
// return an object
// Notice we're not exposing greetFriend, it's private
return {
init: function(name){
return {
name: name,
friends: [],
sayHi: sayHi,
addFriend: addFriend,
greetFriends: greetFriends
}
}
}
}());
var me = Person.init("Steven")
me.addFriend("Sophie")
me.addFriend("Antoin")
me.greetFriends()
me.sayHi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment