Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Comments on Function.prototype.call()

Post

Function.prototype.call()

+2
−1

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);

This worked (console output was a plausible number):

[].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():

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?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Can you provide an example of the HTML? I'm assuming that `.example` refers to the `ul` element, but ... (7 comments)
Can you provide an example of the HTML? I'm assuming that `.example` refers to the `ul` element, but ...
hkotsubo‭ wrote over 2 years ago

Can you provide an example of the HTML? I'm assuming that .example refers to the ul element, but it'd be nice if you could confirm that

hkotsubo‭ wrote over 2 years ago · edited over 2 years ago

Also, if you're testing the code in browser's console, that explains the undefined, because calling console.log(whatever) in browser's console prints the whatever value and also prints the return of console.log function (which is undefined). Anyway, please confirm where/how you're testing this as well (BTW, the same happens in Node's console)

deleted user wrote over 2 years ago

Hi, I can add HTML but it's a long list of li children with much content in the deletion log of Wikipedia, so I prefer not to add it; indeed it's just a ul and li list, that's all the HTML in pattern/in principle, I just misunderstand the MDN documentation about where is the this in the code.

hkotsubo‭ wrote over 2 years ago

You don't need to add everything, just the first 4 or 5 lines or so. I just want to confirm if example is the ul's class

deleted user wrote over 2 years ago

Yes, example and .example refer to the ul (sorry for not mentioning before).

deleted user wrote over 2 years ago

The question is principal so I think no further code is needed.

hkotsubo‭ wrote over 2 years ago

I've updated my answer, now that I confirmed that example is the ul's class