Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Fisher
7,974 PointsCan someone convert this Arrow function to a ES5 style function?
can somebody rewrite this Arrow Function as a ES5 style function? I'm having trouble following how it works and think would help me understand. I've watched the workshop on Arrow Functions but none of the examples really look like the one here.
const myHeading = document.getElementById('myHeading');
const myButton = document.getElementById('myButton');
const myTextInput = document.getElementById('myTextInput');
const resetButton = document.getElementById('resetButton');
myButton.addEventListener('click', () => {
myHeading.style.color = myTextInput.value ;
});
thanks
2 Answers
Steven Parker
231,271 PointsIt's pretty easy to convert back to traditional function notation.
In the case of an anonymous lambda function like this one, just put the keyword "function" in front of the parentheses, and remove the "arrow":
myButton.addEventListener('click', function () {
myHeading.style.color = myTextInput.value ;
});
John Fisher
7,974 PointsThanks. Makes a lot more sense now. I don't think I remember seeing any lambda functions so far on the JavaScript Beginners track so it threw me a bit.
Steven Parker
231,271 PointsMay as well get used to them. They are extremely common in JavaScript.