Hi, how would you explain this way of using methods?
return title
.split(" ")
.filter(substr => substr !== "")
.join("-")
.toLowerCase();
Hi, how would you explain this way of using methods?
return title
.split(" ")
.filter(substr => substr !== "")
.join("-")
.toLowerCase();
Just like the page title, but in url format (look at the url for this page, “methods-written-like-a-list).
return title = “Methods written like a list” – string
.split(” “) = “Methods” “written” “like” “a” “list” – split into words
.filter(substr => substr !== " “) = filter out spaces
.join(”-”) = “Methods-written-like-a-list” – join words
.toLowerCase(); = “methods-written-like-a-list” – to lower case
To add to the above - it is possible to chain methods like that, because each of the called methods returns the changed object itself.
Thank you for all the answers.
By the way this is called “chaining” methods.
return title // this is a string, strings have a `split` method
.split(" ") // this returns an array, arrays have a `filter` method
.filter(substr => substr !== "") // this returns another array, arrays have a `join` method
.join("-") // this returns a string, strings have a `toLowerCase` method
.toLowerCase(); // this returns a string, which is the value that is returned from the function
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.