Skip to content

Instantly share code, notes, and snippets.

@2color
Created January 10, 2019 09:19
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 2color/316ab17bcf1f6aa064de73653bc9df45 to your computer and use it in GitHub Desktop.
Save 2color/316ab17bcf1f6aa064de73653bc9df45 to your computer and use it in GitHub Desktop.
Default method parameters can access object properties
class Person {
constructor (name) {
this.name = name
this.defaultSalultation = 'Dr.'
}
sayHello (salutation = this.defaultSalultation) {
return `Hello ${salutation} ${this.name}`
}
}
const dr1 = new Person('James Baldwin')
console.log(dr1.sayHello('The One and Only Mr.')) // Hello The One and Only Mr. James Baldwin
const dr2 = new Person('David Deutsch')
console.log(dr2.sayHello()) // Hello Dr. David Deutsch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment