Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.