Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I wanted to print to browser console the number of li list items of a given ul list. This didn't work (console output was undefined): console.log(document.querySelectorAll(".example").length); ...
#2: Post edited
- I wanted to print to browser console the number of `li` list items of a given `ul` list.
This didn't work (output was _undefined_):- ```js
- console.log(document.querySelectorAll(".example").length);
- ```
This worked (output was a plausible number):- ```js
- [].filter.call(
- document.getElementsByClassName('example')[0].children,
- function (el) {
- return el.nodeName === 'LI';
- }
- ).length
- ```
- ## My question
- It was written in the [MDN page for Function.prototype.call()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call):
- > The call() method calls a function with a given this value and arguments provided individually.
- But in the working code there was no "this".
- What is the meaning of "call" in the second code?
- I wanted to print to browser console the number of `li` list items of a given `ul` list.
- This didn't work (console output was _undefined_):
- ```js
- console.log(document.querySelectorAll(".example").length);
- ```
- This worked (console output was a plausible number):
- ```js
- [].filter.call(
- document.getElementsByClassName('example')[0].children,
- function (el) {
- return el.nodeName === 'LI';
- }
- ).length
- ```
- ## My question
- It was written in the [MDN page for Function.prototype.call()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call):
- > The call() method calls a function with a given this value and arguments provided individually.
- But in the working code there was no "this".
- What is the meaning of "call" in the second code?
#1: Initial revision
Function.prototype.call()
I wanted to print to browser console the number of `li` list items of a given `ul` list. This didn't work (output was _undefined_): ```js console.log(document.querySelectorAll(".example").length); ``` This worked (output was a plausible number): ```js [].filter.call( document.getElementsByClassName('example')[0].children, function (el) { return el.nodeName === 'LI'; } ).length ``` ## My question It was written in the [MDN page for Function.prototype.call()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call): > The call() method calls a function with a given this value and arguments provided individually. But in the working code there was no "this". What is the meaning of "call" in the second code?