Jump to content

What is context


arun
 Share

Recommended Posts

Hi, friends,

what is context in this code   "game.add.button(x, y, name, callback, context) "

and why this is used in place of context in this code      "this.muteButton = game.add.button(20, 20, 'mute', this.toggleSound,
this);

 

Link to comment
Share on other sites

Usually this is a mechanism to bind functions to a specific scope, usually changing the `this` value of a function.

i.e.

function nextTick (callback, context) {
  setTimeout(callback.bind(context), 0)
}

function log () {
  console.log(this)
}

nextTick(log)
// Window

nextTick(log, {foo: 'bar'})
// {foo: 'bar'}

Note that if you're using anonymous/fat-arrow/lambda functions from ES6 (`() => {}`) then scope works differently.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...