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 trialKristian Woods
23,414 PointsCould someone explain chaining methods in javascript?
I don't quite understand chaining different methods in javascript, and where I can find a reference to different methods.
also, Im not sure if its properties of the selected element or methods.
for example:
const paragraph = document.querySelector(".paragraph");
paragraph.style.color = "red"; // chaining style and color
However, I had trouble understanding the checked method
const checkbox = document.querySelector(".checkbox");
const checked = checkbox.checked // chaining checked method
if(checked == true) {
// do something
} else {
// do something else
}
From what I gather, the checked variable holds a boolean of either true or false.
but I don't understand whats going on
1 Answer
Steven Parker
231,275 PointsThis is not the same thing as "chaining methods".
Generally "chaining methods" refers to when you call methods on the result of other methods. It does look slightly similar in that it also uses dots, but the method calls will have parentheses, possibly with arguments in them.
Here, you are just using the dot notation for selecting an object property. For example, "checked" is a property of a checkbox object, and as you said will either be true or false to show if the box has a mark in it. And "style" is a property of a paragraph object, but it is itself also an object that has a "color" property.
Does that help?
Kristian Woods
23,414 PointsKristian Woods
23,414 PointsOk, great, I have a better idea now. Thanks, Steven