Inside the callback of an event, how do I get the scope outside the function? Specifically, in the following scenario, I want the web3 object: (using truffle-contracts and web3 1.0 here)

const web3 = new Web3(someProvider);

myContract.deployed().then(function(instance) {
    // both ways work
    instance.myEvent({}, function(error, event) {
        // here, I want to use web3

closed as off-topic by Ismael, shane, Achala Dissanayake, Rosco Kalis, Richard Horrocks Jan 12 at 11:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, shane, Achala Dissanayake, Rosco Kalis, Richard Horrocks
If this question can be reworded to fit the rules in the help center, please edit the question.


If you define a variable in Javascript without const or var clause, it will be defined globally. So you can write:

web3 = new Web3(someProvider);

For more information about variables scope in Javascript see: https://www.w3schools.com/js/js_scope.asp

Not the answer you're looking for? Browse other questions tagged or ask your own question.