Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive value returned from constructor will be lost

Tags:

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.

like image 794
Alex Ho Avatar asked Oct 13 '14 11:10

Alex Ho


People also ask

Can a type script constructor return a primitive value?

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.

What would happen if a constructor function explicitly returned an object?

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.

Can a Javascript constructor return a primitive value as a number or a string )?

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.


1 Answers

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.

like image 121
Matt234 Avatar answered Sep 28 '22 22:09

Matt234