Introduction to the different _javascript techniques of alert () and Console.log ()

Source: Internet
Author: User

Simply say alert is a pop-up prompt and Console.log is logged in the Debug tool, and here's a list of the different points of alert () and Console.log ().

[1]alert ()

[1.1] Blocking action, no click OK, subsequent code cannot continue execution

[1.2]alert () can output only string if alert outputs an object that automatically calls the ToString () method

e.g. alert ([1,2,3]);//' 1,2,3 '

[1.3]alert does not support the writing of multiple parameters, only the first value can be output

e.g. alert (1,2,3);//1

[2]console.log ()

[2.1] On the print table output

[2.2] can print any type of data

e.g. console.log ([1,2,3]);//[1,2,3]

[2.3] To support multiple parameters of the wording

e.g Console.log (1,2,3)//1 2 3

The ToString () method outputs alert () and Console.log () in the prototype chain to get different results

<script type= "Text/javascript" >
var a = [1,2,3];
alert (a); 1,2,3
Array.prototype.toString = function () {return
  ' str ';
}
alert (a); STR
</script>

And

<script type= "Text/javascript" >
var a = [1,2,3];
Console.log (a); [1,2,3]
Array.prototype.toString = function () {return
  ' str ';
}
Console.log (a); [1,2,3]
</script>

The results of the above code output are different for the following reasons:

Console.log () can print any type of data. Alert () can only output a string, and the ToString () method is automatically invoked if the alert output is an object. If you want to Console.log () output the same as alert, you need to call ToString ():

Console.log (obj. toString ());

It has nothing to do with the way you write ToString (), the Tostrong () you write yourself simply rewrites the object's default ToString () method.

If you do not rewrite the ToString () method, alert () also invokes the default.

Or the sentence: Console.log () can print any type of data, and it will be invoked because you have rewritten ToString (). If log () can only print a string, then the console log method is not necessary.

Basically, the data types that two functions expect are different. Alert () expects a string type for the data type. This is tantamount to using the object in a string context and naturally making the corresponding conversion. Console.log () is clearly acceptable to any type of data. Then he wouldn't have to switch. That is to say, not in string context. That obj was naturally his first data type.

The above list of alert () and Console.log () different, have different ideas of friends, welcome to propose, share to everyone, learn and progress together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.