Datatypes and javascript ... what a nightmare

So..

I pass data to a function that processes strings and numbers differently.

I would like to pass an array of values ​​and determine what the types of each value are.

row[0] = 23; row[1] = "this is a string... look at it be a string!"; row[2] = true; $.each(row, function(){ alert(typeof(this)); //alerts object }); 

Is it possible to determine the "actual" data types in a given string?

+4
source share
2 answers

Try

 var row = [ 23, "this is a string", true ]; $.each(row, function (index,item) { alert(typeof(item)); }); // Alerts "number", "string", "boolean" 

Whenever possible, I try to avoid using "this" in callbacks, and using explicit arguments is usually clearer and more predictable.

+5
source

@Rich suggested the best possible solution is to use the values ​​passed to the callback as arguments. Quote from jQuery doc :

This value can also be accessed using the this , but Javascript will always carry this value as an Object , even if it's a simple string or a numeric value.

this.valueOf() can help you return to a primitive value. But still - in this specific example, it is better to use the values ​​passed as arguments to the function.

+3
source

Source: https://habr.com/ru/post/1301618/


All Articles