0

According to this youtube tutorial, if I use this inside addEventListener it should be relative to the callback function and will not have access to this in the highest level inside the class if I don't use binding. Why do I get the same result when I console.log this inside and outside the callback even without the binding?

Here's the code:

class FormValidator {
  constructor(form, fields) {
    this.form = form;
    this.fields = fields;
  }

  initialize() {
    this.validateOnSubmit();
  }

  validateOnSubmit() {
    console.log(this);

    this.form.addEventListener('submit', e => {
      e.preventDefault();
      console.log(this);
    });
  }
}

const form = document.querySelector('.form');
const fields = ["username", "email", "password", "password_confirmation"];

const validator = new FormValidator(form, fields);

validator.initialize();
* {
  box-sizing: border-box;
}

body {
  background-color: blueviolet;
}

.title {
  margin-bottom: 2rem;
}

.hidden {
  display: none;
}

.icon {
  width: 24px;
  height: 24px;
  position: absolute;
  top: 32px;
  right: 5px;
  pointer-events: none;
  z-index: 2;
}

.icon.icon-success {
  fill: green;
}

.icon.icon-error {
  fill: red;
}

.container {
  max-width: 460px;
  margin: 3rem auto;
  padding: 3rem;
  border: 1px solid #ddd;
  border-radius: 0.25rem;
  background-color: white;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.label {
  font-weight: bold;
  display: block;
  color: #333;
  margin-bottom: 0.25rem;
  color: #2d3748;
}

.input {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  color: #2d3748;
  border: 1px solid #cbd5e0;
  line-height: 1.25;
  background-color: white;
  padding: 0.65rem 0.75rem;
  border-radius: 0.25rem;
  box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
}

.input::-moz-placeholder {
  color: #a0aec0;
}

.input:-ms-input-placeholder {
  color: #a0aec0;
}

.input::placeholder {
  color: #a0aec0;
}

.input.input-error {
  border: 1px solid red;
}

.input.input-error:focus {
  border: 1px solid red;
}

.input:focus {
  outline: none;
  border: 1px solid #a0aec0;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
  background-clip: padding-box;
}

.input-group {
  margin-bottom: 2rem;
  position: relative;
}

.error-message {
  font-size: 0.85rem;
  color: red;
}

.button {
  background-color: blueviolet;
  padding: 1rem 2rem;
  border: none;
  border-radius: 0.25rem;
  color: white;
  font-weight: bold;
  display: block;
  width: 100%;
  text-align: center;
  cursor: pointer;
}

.button:hover {
  filter: brightness(110%);
}

.promo {
  color: white;
  opacity: 0.75;
  margin: 1rem auto;
  max-width: 460px;
  background: rgba(255, 255, 255, 0.2);
  padding: 20px;
  border-radius: 0.25rem;
}

.promo a {
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./styles.css">
  <title>Sandbox</title>
</head>

<body>
  <div class="container">
    <h2 class="title">Create a new account</h2>
    <form action="#" class="form">

      <div class="input-group">
        <label for="username" class="label">Username</label>
        <input id="username" placeholder="webcrunch" type="text" class="input">
        <span class="error-message"></span>
        <svg class="icon icon-success hidden hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
            clip-rule="evenodd" />
        </svg>

        <svg class="icon icon-error hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
            clip-rule="evenodd" />
        </svg>
      </div>

      <div class="input-group">
        <label for="email" class="label">Email</label>
        <input id="email" type="email" class="input" autocomplete placeholder="andy@web-crunch.com">
        <span class="error-message"></span>
        <svg class="icon icon-success hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
            clip-rule="evenodd" />
        </svg>

        <svg class="icon icon-error hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
            clip-rule="evenodd" />
        </svg>
      </div>

      <div class="input-group">
        <label for="password" class="label">Password</label>
        <input id="password" type="password" class="input">
        <span class="error-message"></span>
        <svg class="icon icon-success hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
            clip-rule="evenodd" />
        </svg>

        <svg class="icon icon-error hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
            clip-rule="evenodd" />
        </svg>
      </div>

      <div class="input-group">
        <label for="password_confirmation" class="label">Password Confirmation</label>
        <input id="password_confirmation" type="password" class="input">
        <span class="error-message"></span>
        <svg class="icon icon-success hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
            clip-rule="evenodd" />
        </svg>

        <svg class="icon icon-error hidden" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
            clip-rule="evenodd" />
        </svg>
      </div>

      <input type="submit" class="button" value="Create account">

    </form>
  </div>

  <div class="promo"> Check out my <a href="https://web-crunch.com" target="_blank">blog</a> or my <a href="https://youtube.com/webcrunch">YouTube channel</a> to learn more about how I made this.</div>

  <script src="/scripts.js"></script>
</body>

</html>
Lance
  • 11
  • 2

0 Answers0