简体   繁体   中英

jquery console.log and alert different value

I realy cant understand javascript. Maybe someone can explain me the difference:

validationErrors[value.element.name] = value.method;
console.log(validationErrors);
alert(validationErrors);

console.log(validationErrors) returns well formed array with values, and alert(validationErrors) returns empty array. Why?

The console is more of a debugging environment and can understand the JS objects that are passed in the log function.

Alert on the other hand is a dialog box and will coerce its arguments into string values. Which is why the output is not as well formatted as the console.

Here is a little snippet of what is actually happening in the alert box.

var validationErrors = [ 2, 3, 4 ];
console.log(toString(validationErrors));
Output >> "[object Window]"

It's also best practice to use the console for logging purposes and not the alert box.

你可以试试这个

alert(JSON.stringify(validationErrors));

Alert have some limit according to browser it varies but most You'd probably be best staying under 1000 characters though, as many browsers seem to begin to truncate after 999. And if you are giving object in alert it will never reflect the values so prefer console for object and other data type can be used in alert but its not a good practice. console give proper view of data like object in array can be studied easily in console.

Alert is used to print messages, which means it is used to display string values. When you pass anything other than string to alert, it calls .toString function of it and prints the output.

Now why did you get a blank string?

An array is supposed to have indexed values only. So you can do array[index] = "bla bla" . But when you do array["something"] = "something else" , it adds a property to that array (since arrays are also objects in Javascript).

According to MDN

The toString() method returns a string representing the specified array and its elements.

In simple, it will loop over length of array and join all elements with ,(comma)

But you do not have elements in it. You have set properties. So length is 0 and hence it returns ""

Following is a simulation

 var a = []; a["test"] = "foo"; console.log(a); console.log(a.toString()); alert({}) 

Reference

All objects in Javascript are passed by reference. So when you passed something to console.log() and it would be changed in code further, in console you will see changed value. On other hand, alert() displays message box and stops code execution, so in message box you see values exactly as they are at alert()'s call time.

For debugging purposes, you may want to use browser's debugger - I recommend Chrome Dev tools . There is free course from codeschool.com to help you discover such great tool: https://www.codeschool.com/courses/discover-devtools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM