I've been playing around ES6 features for sometime now and on this very occasion, there was an error of me using the function before declaring it, but when I changed to the good old traditional way, the error wasn't caught.
Does function hoisting only work for the traditional way, or there's a catch I'm missing?
I've tried searching but this Besides syntax, is there any difference between a normal function and an arrow function? couldn't give me a satisfactory result
A simple input form code:
<!DOCTYPE html>
<html>
<head>
<title>Hero Form</title>
</head>
<body>
<form id="hero">
<label for="heroName">Name:
<input type="text" name="heroName" autofocus
placeholder="Your super Hero Name"Smaxlength="32">
<!-- </label> -->
<button id="submit" type="submit">Submit</button>
</form>
</body>
</html>
<script>
// Throws an error when called first
form.addEventListener('submit', makeHero, false);
const form = document.forms.hero;
const makeHero = (event) => {
event.preventDefault();
const hero = {};
hero.name = form.heroName.value;
console.log(JSON.stringify(hero), hero, 'clear');
return hero;
}
</script>
<script>
const form = document.forms.hero;
const makeHero = (event) => {
event.preventDefault();
const hero = {};
hero.name = form.heroName.value;
console.log(JSON.stringify(hero), hero, 'clear');
return hero;
}
// Works fine after arrow function declaration
form.addEventListener('submit', makeHero, false);
</script>