Skip to content

Instantly share code, notes, and snippets.

@dijs
Created October 26, 2015 17:58
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 dijs/b5f43dea79ab83f12e43 to your computer and use it in GitHub Desktop.
Save dijs/b5f43dea79ab83f12e43 to your computer and use it in GitHub Desktop.
Don't destruct stateful methods

Here is my simple example:

class Speaker {
  constructor(name) {
   this.name = name 
  }  
  say(msg) {
    console.log(`${this.name}: ${msg}`)
  }
}

let speaker = new Speaker('John')

speaker.say('hi') // Logs as expected, John: hi

let {say} = speaker

say('hi') // Uncaught TypeError: Cannot read property 'name' of undefined

// Bind fix... but should never use
// say = say.bind(speaker)

Probably a good rule to never pull methods from objects, unless they are stateless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment