Closed KTS915 closed 8 months ago
I think it's not equivalent, because there is no error checking in the plain version. The jQuery functions don't generate notices when the selector is not found. Plain JS will.
Doesn't that make the plain JavaScript version better? The selector should always be available, so the code should throw an error if it isn't.
I guess there are two interpretations of "better". I would label no errors better than possible errors. I have written code just like yours, thinking the element should always be there, and then something unrelated changes or a scenario I never thought of happens, and my code breaks (gives an error).
I wonder if the original is even accessible. Why is this code in the page, anyway?
I wonder if the original is even accessible.
It isn't accessible. But that's a matter for a different PR.
I would label no errors better than possible errors.
Of course. But that's not the comparison being made here. What we're talking about here is if there is an error, should it be reported. To which the answer surely is an emphatic yes. So the vanilla version is superior.
The selector should always be present because it's created by this very plugin. If it doesn't exist, then that is very definitely an error that needs to be reported, not suppressed or overlooked.
The jQuery version doesn't suppress or overlook the error. It checks for it, whereas the vanilla does not. The event of an error should be handled, not reported, especially not to the user, who can't do anything about it.
No, it just ensures that the code can't run when the selector doesn't exist. But there is an unreported error there, because the code should exist. Of course the user needs to see it because they need to be able to report the error message to a developer. We all know that "doesn't work" is a pretty useless report.
All I'm saying is that the vanilla is not equivalent to the jQery. If you add more checks, you can make it equivalent or better (by handling the error better).
Just a suggestion to change from jQuery to plain JavaScript.