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

jQuery.each is generating a bug with a string parameter #3966

Closed
brabia opened this issue Feb 7, 2018 · 3 comments
Closed

jQuery.each is generating a bug with a string parameter #3966

brabia opened this issue Feb 7, 2018 · 3 comments

Comments

@brabia
Copy link

brabia commented Feb 7, 2018

jQuery version

3.1.1

Description

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in this is a string
jQuery.each is generating a bug with a string parameter http://prntscr.com/ib9ngg

How to reproduce it

var jEach = function(a){
	jQuery.each(a, function(){
		console.log(this);
	});
};

jEach(null);
jEach([1,2]);
jEach('[1,2]');
jEach('this is a string');

@mgol
Copy link
Member

mgol commented Feb 7, 2018

Strings are not supported parameters to jQuery.each, only objects or arrays. You may only pass parameters documented on https://api.jquery.com, e.g. here https://api.jquery.com/jQuery.each/.

@mgol mgol closed this as completed Feb 7, 2018
@brabia
Copy link
Author

brabia commented Feb 7, 2018

Thanks for your quick response

I know that jQuery.each is supporting an array or an array of objects but by accident instead of using the proper parameter which should be an array in this case, I used a string, so I guess jQuery.each should do nothing here instead of firing an error message.

Thanks again.

@mgol
Copy link
Member

mgol commented Feb 7, 2018

jQuery in general doesn't validate its inputs, it just relies on them being correct. Adding consistent validation would increase the library size a lot.

If you want such validation, consider using TypeScript which contains jQuery type definitions in the @types/jquery package.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants