Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the role of the 2nd argument in this function? #30

Closed
VincentTam opened this issue Aug 12, 2019 · 2 comments

Comments

@VincentTam
Copy link
Collaborator

commented Aug 12, 2019

In

function elem(selector, parent = document){
let elem = document.querySelector(selector);
return elem != false ? elem : false;
}
, parent is never used. Perhaps there's a mistake.

@VincentTam VincentTam added the question label Aug 12, 2019

@onweru

This comment has been minimized.

Copy link
Owner

commented Aug 12, 2019

I used parent purposely for a scenario where you need to be more specific. That is, in a scenario where you want to query from a different node other than the document.

For example, we could have:

let parent, child, greatGrandChild;
parent = document.querySelector('.parent');
// search for list items within the parent node. They could be nested several levels deep
greatGrandChild = parent.querySelector('li');

As you may have noticed, parent is set to document by default. Thusly, it's optional.

I was trying to replicate the .find() function in jQuery. I believe it uses the same principle.

@onweru

This comment has been minimized.

Copy link
Owner

commented Aug 12, 2019

I used parent purposely for a scenario where you need to be more specific. That is, in a scenario where you want to query from a different node other than the document.

For example, we could have:

let parent, child, greatGrandChild;
parent = document.querySelector('.parent');
// search for list items within the parent node. They could be nested several levels deep
greatGrandChild = parent.querySelector('li');

As you may have noticed, parent is set to document by default. Thusly, it's optional.

I was trying to replicate the .find() function in jQuery. I believe it uses the same principle.

You're right, there's a slight mistake. Thanks for bringing it to my attention

onweru added a commit that referenced this issue Aug 13, 2019

@onweru onweru closed this Aug 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.