I created a function:
function CheckHidden(el){ return $(el).css('display')!='none' }
My IDE warns me that:
Primitive value returned from constructor will be lost when called with 'new'
Actually when I call it like this:
var all = $("#catalog-body > div").filter(function(){return CheckHidden(this)})
it doesn't work and doesn't reduce set of elements to unhidden ones. Please explain to me the issue. I have a giant lack of knowledge.
Code Inspection: Constructor returns primitive value When called with new , this value will be lost and an object will be returned instead. To avoid warnings, use the @return tag to specify the return of the function.
Return from constructors Their task is to write all necessary stuff into this , and it automatically becomes the result. But if there is a return statement, then the rule is simple: If return is called with an object, then the object is returned instead of this . If return is called with a primitive, it's ignored.
Basically if your constructor returns a primitive value, such as a string, number, boolean, null or undefined, (or you don't return anything which is equivalent to returning undefined ), a newly created object that inherits from the constructor's prototype will be returned.
I ran into this warning myself and if you want to know the cause, it is because your IDE expect function names to start with a lowercase letter. Since your function is called CheckHidden with a capital C, it thinks it's a class declaration.
However, you should still use jQuery's :visible selector to fix your specific issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With