Extract refactor
It's common to realize that a certain block of code is going to be used in multiple places. When you realize that, you usually want to extract this code into it's own method. VS Code includes an "Extract Method" refactoring tool to make this easier.
Exercise
Answer
Extract the code in the connection.on method inside the constructor into a method called updateColor
    Open the "app.js" file
    Highlight the code inside of the connection.on AFTER the "// update the bulb color" comment. Do not include the comment line.
1
// receives the "colorChanged" web socket event
2
connection.on("colorChanged", hex => {
3
// update the bulb color
4
bulb.style = `fill: #${hex};`;
5
currentColor.textContent = `#${hex}`
6
});
Copied!
    Press Cmd/Ctrl + .
    Select "Extract method in class "Application"
    Name the method "updateColor"
    Press Enter
    Notice that the code has been extracted to a new method in the Application class called updateColor. Also notice that the variable hex was automatically included as a parameter.
Last modified 2yr ago
Copy link