Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

JavaScript - how to access this object inside event method?

0 contributions
2 points

Most common problem for new developers that try programming in JavaScript is usage this in propper way. In JavaScript it is possible to access object inside call back in following ways.

1. self / additional variable example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button id="clicker">Click me!</button>
  <script>

    var handle = document.getElementById('clicker');
    
    var MessageManager = {
      message: 'This is example message...',
      assignClickEvent: function(handle) {
          var self = this;
          
          handle.addEventListener('click', function() {
            alert(self.message);
          });
      }
    };

    MessageManager.assignClickEvent(handle);
    
  </script>
</body>
</html>

Note: this used inside click event function indicates for button element to solve this problem self variable has been used.

Result:

Function and self variable usage example
Function and self variable usage example

2. Function bind method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button id="clicker">Click me!</button>
  <script>

    var handle = document.getElementById('clicker');
    
    var MessageManager = {
      message: 'This is example message...',
      assignClickEvent: function(handle) {

          function onClick() {
            alert(this.message);
          }

          handle.addEventListener('click', onClick.bind(this));
      }
    };

    MessageManager.assignClickEvent(handle);
    
  </script>
</body>
</html>

Note: by using onClick.bind method we create proxy instance for onClick function that will be called later with new context - in this case this indicates to MessageManager object.

Result:

Function bind method and this example
Function bind method and this example

3. ES6 and arrow function example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <button id="clicker">Click me!</button>
  <script>

    var handle = document.getElementById('clicker');
    
    var MessageManager = {
      message: 'This is example message...',
      assignClickEvent: function(handle) {

          handle.addEventListener('click', () => {
            alert(this.message);
          });
      }
    };

    MessageManager.assignClickEvent(handle);
    
  </script>
</body>
</html>

Note: with arrow function we can use still this with parent context.

Result:

Arrow function with this context example
Arrow function with this context example
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more