"JavaScript advanced Programming" Reading notes-(2) Basic concepts

Source: Internet
Author: User

Variable

Javascript is case-sensitive, meaning that Var nun and Var Num are different variables.

The ECMAScript variable is loosely typed, and the so-called loose type is the ability to save any type of data. There are 6 types of variables in ECMAScript: 5 basic types and 1 reference types.

The basic types are as follows:

    1. Undefined;
    2. Null;
    3. Boolean;
    4. number;
    5. String;

There is only one type of reference, and that is object. The sample code looks like this:

var message;console.log (message); // scalar that is declared but not initialized is undefined var str= ' I have a dream! ' ; str= 10; // A variable in JavaScript is a weak type, and a variable can store any type of data Console.log (str);

The output results are as follows:

Passing parameters

In JavaScript, if a function pass parameter is a basic type, the function copies the basic type parameter (equivalent to a copy), and if the passed argument is a reference type (that is, the object), the object is referenced inside the function (passing is essentially the address of the object). The sample code looks like this:

var x=10,y={name: ' Binghuojxj '}; function Test (x, y) {    x+=1;    Console.log (' function inside x= ' +x);    Y.address= ' Jinan Lixia District '; Console.log (' inside the function Y. Address= ' +y.address);        } Test (x, y); Console.log (' function external x= ' +x); Console.log (' external ' function Y. Address= ' +y.address);

The output results are as follows:

function parameters

Because there is no feature of function signature, the ECMAScript function cannot be overloaded;

You can pass any number of arguments to the ECMAScript function, which is independent of the number of arguments that are defined by the function, and you can access them through the arguments object; The sample code is as follows:

function Add () {
  var sum=0;   
   for (var i=0;i<arguments. length; i++)
  {
      sum+=arguments[i];
  }
  return sum;
}
Console.log (ADD (10,20));
Console.log (ADD (10,20,30));
Console.log (ADD (10,20,30,40));

The output results are as follows:

You do not need to specify a function's return value, because any ECMAScript function can return any value at any time.

JavaScript does not have block-level scopes

Do not say anything, directly on the code:

if (1==true)
{
   var color= ' Blue ';
}
Console.log (' If statement external color= ' +color);
 for (var x=0;x<10;x++)
{
   Console.log (' +x ');
}
Console.log (' For loop external x= ' +x);
The output is shown as follows:

0.1+0.2 Not equal to 0.3

Run the following code:

Alert (0.1+0.2);

The output results are as follows:

The reasons are as follows:

The numbers in JavaScript are represented by floating-point number, and are specified as double-precision floating-point numbers using the IEEE 754 standard.

IEEE 754 specifies two basic floating-point formats: single-precision and double-precision.

    1. The IEEE single-precision format has 24 digits of significant numeric precision (including symbol numbers) and occupies a total of 32 bits.
    2. The IEEE double format has 53 digits of significant numeric precision (including symbol numbers) and occupies a total of 64 bits.

Push to the procedure as shown below:

Decimal 0.1  
= = Binary 0.00011001100110011 ... (Cycle 0011)   
= = Mantissa is 1.1001100110011001100 ... 1100 (total 52 digits, except for the left 1 of the decimal point), exponent 4 (binary shift code 00000000010), sign bit 0  
= = Computer Storage: 0 00000000100 10011001100110011 ... 11001  
= = Because the mantissa is up to 52 bits, the actual stored value is 0.00011001100110011001100110011001100110011001100110011001  
and the decimal 0.2  
= = Binary 0.0011001100110011 ... (Cycle 0011)  
= = Mantissa is 1.1001100110011001100 ... 1100 (total 52 digits, except for the left 1 of the decimal point), exponent 3 (binary shift code 00000000011), sign bit 0  
= = Storage: 0 00000000011 10011001100110011 ... 11001  
Because the mantissa is up to 52 bits, the actual stored value is 0.00110011001100110011001100110011001100110011001100110011  
Then the two add up to:      
0.00011001100110011001100110011001100110011001100110011001  
+  0.00110011001100110011001100110011001100110011001100110011
=  0.01001100110011001100110011001100110011001100110011001100  
Converted to 10-binary, get: 0.30000000000000004   

"JavaScript advanced Programming" Reading notes-(2) Basic concepts

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.