Created
May 31, 2016 16:16
-
-
Save octosteve/dacc0b1e14782a6a73338ca7c753eb41 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
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