arun Posted February 15, 2018 Share Posted February 15, 2018 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 More sharing options...
Fenopiù Posted February 15, 2018 Share Posted February 15, 2018 Context is where the object "exist". game/this are most common context. Link to comment Share on other sites More sharing options...
mattstyles Posted February 15, 2018 Share Posted February 15, 2018 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 More sharing options...
Recommended Posts