Created
November 21, 2011 01:34
-
-
Save jrmoran/1381370 to your computer and use it in GitHub Desktop.
Privileged methods CoffeeScript
This file contains hidden or 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
class Person | |
constructor: (name)-> | |
@name = name | |
# private method | |
greet_p = (person = {}) -> | |
msg = if person.hasOwnProperty 'name' | |
"Hi #{person.name}" | |
else | |
"Hi" | |
"#{msg}, I'm #{@name}" | |
# privileged method | |
greet: (person)-> | |
greet_p.call this , person | |
console.log '***' | |
john = new Person 'John' | |
bob = new Person 'Bob' | |
console.log john.greet() # Hi, I'm John | |
console.log bob.greet() # Hi, I'm Bob | |
console.log john.greet bob # Hi Bob, I'm John | |
console.log bob.greet john # Hi John, I'm Bob | |
console.log john.greet_p() # Object #<Person> has no method 'greet_p' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment