What you don't know about JavaScript (1)

Source: Internet
Author: User

Confusions 1:

Let's take a look at an example:

function test(){message = "hi";}test();alert(message);
The output string "hi"

In a function, the variable defined by var is a local variable, and the variable omitted by the var operator is a global variable.

Confusions 2:

alert(undefined == null)
The result is "true"

We know that js contains basic types and reference types, including number, string, boolean, undefined, and null. if a basic type is not initialized, it will be of the undefined type, and null indicates a null pointer. In fact, the undefined value is derived from null, so true is returned.

Confused 3:

Alert (isNaN (NaN); // truealert (isNaN (10); // falsealert (isNaN ("10 ")); // false automatic type conversion alert (isNaN ("blue"); // truealert (isNaN (true); // false automatic type conversion
NaN indicates that a non-a Number is a special value. In ECMAScript, if any value is divided by 0, NaN is returned, and any operation involving NaN will return NaN. naN is different from any value, including itself.

alert(NaN == NaN);  // false 
Confused 4:

for(var propName in window){document.write(propName);}
The For-in statement is a precise iterative statement that can be used to enumerate the attributes of an object, similar to for (String s: String []) in java.

Confusions 5:

Var qs = location. search. substring (1); var hostName = location. hostname; var url = location. href; // equivalent to the following with (location) {var qs = search. substring (1); var hostName = hostname; var url = href ;}
The with statement is used to set the code scope to a specific object to simplify the work of writing the same object multiple times.

Puzzles 6:

function howManyArgs(){alert(arguments.length);}howManyArgs("String", 45);  //2howManyArgs();  //0howManyArgs(12);   //1
The parameters in ECMAScript are represented by an array internally. The function always receives this array, regardless of the parameters contained in the array (if no parameter exists ).

Naming parameters are only convenient, but not necessary. Let's look at the example below.

function doAdd(num1, num2){arguments[1] = 10;alert(arguments[0] + num2);}
Because the values in the arguments object are automatically reflected in the corresponding named parameters, num2 is changed to 10, but their memory space is independent (not referenced ), in addition, the length of the arguments object is determined by the number of input parameters, not by the number of named parameters when defining the function.

Puzzles 7:

For (var I = 0; I <10; I ++) {} alert (I); // The output result is 10.
JavaScript has no block-level scope.




Related Article

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.