JavaScript authoritative Guide (6th edition) Learning Note one

Source: Internet
Author: User
Tags square root

Tag: His must change text not using character set minimum scope cal

2nd Chapter Lexical Structure

One, Character set

The *javascript program is written in the Unicode character set.

*javascript are strictly case sensitive.

*javascript notes: (1) "//" for single-line comments, (2) "/* ..../" for multiline comments.

*javascript separates statements by using semicolons, and you can usually omit semicolons if each statement is a single line. But here are a few things to note:

A = 3;  // Here the semicolon is can be omitted b = 4= 3;  b = 4;   // here the first semicolon cannot be omitted

If you write without a semicolon, JavaScript automatically fills the semicolon, but only when the semicolon is missing and the code is not parsed correctly.

var y = x + F (a+b). toString ()// for the above code parsing automatically adds a semicolon following var y = x + f (a+
   
    b). toString (); 
    // 
    It's not the intention.
   
x+ +y// Parse Auto-add after  ++y;

So, adding semicolons is a good writing habit.

3rd chapter types, values and variables

*javascript data types are divided into two categories: primitive type (primitive type) and object type, where the original type includes numbers, strings, Boolean values, null (NULL), undefined (undefined), and JavaScript objects are named values Unordered collection, which is a collection of attributes (property) that are subordinate to the object (Obeject), each of which is named by a "name/value pair" (where the value can be either the original type data or the object type data); JavaScript also defines special objects such as arrays (array) and functions.

The *JAVASCRIPT interpreter has its own memory management mechanism that automatically recycles memory (garbage collection), and the interpreter automatically reclaims the memory space occupied by the object when no longer any references in the program point to an object.

* From another angle, the JavaScript data type can be divided into mutable types and immutable types. The value of a mutable type can be modified, such as an array, an object. Numbers, Booleans, nulls, and undefined are non-mutable types.

First, the number

*javascript does not distinguish between integers and floating-point numbers, all of which are represented by a floating-point number. When a number appears directly in the program, it is called a Digital direct quantity (numeric literal).

*javascript is free to convert data types

The Math object is defined in *javascript and its properties and methods define a number of arithmetic operations:

//2 for 53 power
Math.pow (2,53)
//rounding, Output 1.0Math.Round (. 6)//rounding up, Output 1.0Math.ceil (. 6)//rounding down, output 0.0Math.floor (. 6)//returns the maximum valueMath.max (x, Y, z)//returns the minimum valuemath.min (x, Y, z)//generates a pseudo-random number between 0 and 1math.random ()//square root of 3MATH.SQRT (3)//three power of eMath.exp (3)

In JavaScript, the overflow of arithmetic operation will not error, Infinity is expressed with infinity, negative infinity is represented by-infinity.

The variable is untyped and can be given any type of value after the declaration.

* Date and time:

varthen=NewDate (2011,0,1);//January 1, 2011varLater=NewDate (2011,0,1,17,10,30);varnow=NewDate ();//Current Date TimeLater.getfullyear ();//OutputLater.getfullmonth;//output 0, month starting from 0Later.getfulldate ();//Output 1Later.getday ();//output 5, Friday; 0 represents Sunday. Later.gethours ();//Output Time

Second, string

A text string is an immutable ordered sequence that consists of a set of 16-bit values. The subscript index of the string starts at 0. Two strings can be understood by the plus "+" operator

msg= "Hello," + "world"; // output "Hello, World"

The main properties and methods of the string:

vars= "Hello, World"s.length//OutputS.charat (0)//Output HS.charat (s.length-1)//Output DS.substring (1,4)//Output 第2-4个 charactersS.slice (-3)//The last three characters of outputS.indexof ("L")//Output 2, where the character L first appearsS.lastindexof ("L")//output 10, where the character last appearsS.indexof ("L", 3)//Output 3, the position where the character first appears after position 3 and afterS.split (",")//["Hello", "World"], split into substringsS.replace ("H", "H")//Replace, output "Hello, World"S.touppercase ()//output "HELLO, World"

Strings are fixed in JavaScript, and the Replace () and toUpperCase () methods above actually return the newly created string.

Three, Boolean value

* The Boolean value has only two values: True and False. Typically, the return value of a comparison statement is a Boolean value. such as "a==4" is used to monitor the value of a is equal to 4.

* The value of any JavaScript can be converted to a Boolean value. undefined/null/0/-0/nan/"" (empty string) is converted to false. All other values, including objects (arrays), will be converted to true.

* Boolean operations: Logic with &&/logic or | | /Boolean not not.

Iv. Global Objects

When the JavaScript interpreter starts (or any web browser loads a new page), it creates a new global object and gives it a defined set of initial properties: Global properties such as Undefined, infinity, and Nan; global functions such as isNaN (), Parsinit (), constructor functions such as date (), REGEXP (), global objects such as math, and so on.

In client-side JavaScript, in all JavaScript code in the browser window it represents, the window object acts as a global object that has a property window that references itself and can refer to the global object in place of this. If the code also declares a global variable, then the global variable is a property of the global object.

Five, packaging objects

var s= "Hello World"; var word=s.substring (S.indexof ("") +1,s.length);

Here S is a string, not an object, but it can still have properties, because JavaScript automatically calls the new string () method when the property referencing the string is converted to an object. Similarly, objects are created through the number () and Boolean () constructors when similar scenes are to numbers and Booleans.

It is important to note that once the attribute reference is finished, the newly created object is destroyed.

var s= "test";    // creates a string s.len=4;           // set a property for it var t=s.len;      // Create variable t

In the above code, the value of the final T is undefined. After the second line of code, the wrapper object for the second line of code that was created temporarily has the Len attribute (note that it does not own the Length property!). ) has been destroyed, the third line continues to create a new wrapper object that does not have the Len attribute.

For the wrapper object created based on the original value, the "= =" is established, but the congruent "= = =" is not established. The "typeof" operator allows you to see the difference in type.

Six, immutable primitive values and mutable object references

Primitive values in JavaScript (numbers, Booleans, strings, nulls, undefined) are fundamentally different from objects, that is, they are immutable, and no method can change a primitive value. This is well understood for undefined, null, numeric, and Boolean values. However, for a string to appear not obvious enough, it is always thought that it can be treated as an array-like object, thus changing the character value. In fact, JavaScript prohibits this. The string method modifies the string that actually returns a new string after the modification.

var s= "Hello"; s.touppercase ();   // this returns "HELLO", temporarily creating a new object storage s;       // output "Hello", not changed

The comparison of the original values is the comparison of values, which is equal only if their values are equal, and for two separate strings, equal if and only if they are of equal length and each corresponding index has equal characters.

The comparison of objects is not a comparison of values, even if two objects contain the same attributes and correspond to the same values, and for two separate arrays. Only two objects are equal if they refer to the same base object.

varO={x:1}, p={x:1};//two separate objects createdO===p;//output false; Two separate objects are never equalvara=[],b=[]; A===b;//output false; Array is also the same as above//case of referencing an objectvarA=[];//Create an arrayvarB=a;//Create an array that references avarC=a;//Create an array that references aC[0]=1;//to modify a reference by CA[0];//Output 1B[0];//Output 1A===b;//trueC===a;//trueB===c;//true

Seven, variable declaration

JavaScript should be declared before using a variable:

var i; var a,b,c;   // You can also declare multiple variables at once var m=1,s= "Hello", T; var n=10;  // declares a variable that creates a basic numeric type nn= "Love";   // can also flexibly transform its data type

Viii. Scope of variables

A variable's scope (scope) is the area in the program's source code that defines the variable. Global variables have global scope; the variables declared within the function are valid only in the body of the function, local variables, and the parameters of the functions are local variables.

Inside a function body, a local variable takes precedence over a global variable of the same name. If a local variable declared within a function or a variable in a function parameter has the same name as a global variable, the global variable is overwritten with a local variable within the function.

var scope= "global";   // declaring a global variable function Checkscope () {    var scope= "local";       // Local Variables    in the function body return scope;} Checkscope ();          // return to "local"

Note that if you assign a value to an undeclared variable, JavaScript actually creates a property of the same name for the global object, making it look like a global variable. Therefore, although you can create a global object without declaring it within the global scope, you must use the VAR statement when declaring a local variable:

Scope= "global";  // Create a global variable function checkscope2 () {    scope= "local";   // "Var" is not used here, the interpreter considers the global variable above    Myscope= "local";  // "Var" is not used, a global variable    is created return [Scope,myscope];  // returns a value of two }checkscope2 ();   // return ["Local", "Loacl"]scope;  // return to "local" // return to "local"

Local-scope nesting scenarios:

 var  scope= "global scope"; //  global variable  function    Checkscope () { var  scope= "Loacl scope"; //     local variable  function    nested () { var  scope= "nested scope"; //         local variables within nested scopes      return  scope; //  Returns the value in the current scope   " return      Nested (); }checkscope ();  

* In programming languages like the C language, each piece of code within the curly braces has its own scope, and the variables declared in it are not visible to the outside, which is the block scope, but the function scope is used in JavaScript SCOPE): A variable is visible in the body of the function that declares it and in any function nested within the body of the function. So in a function body, no matter where the variable is declared or even before it is declared, it can be used (without prompting for an error), but only after it has been declared and assigned, can it use the value assigned to it correctly. Therefore, in general, a good habit is to put the local variables to be declared in the function body at the top of the function body.

var scope= "Global"; function f () {    // output "undefined", visible is the local variable declared later, not the global variable    // But because this local variable has not been assigned a value         Console.log (scope);       // Local variables are assigned here, but are visible       anywhere within the function body var scope= "local";      Console.log (scope);    // output "local"}

* The difference between implicit and explicit declarations of global variables is that global variables declared using the VAR statement explicitly cannot be deleted, and implicitly declared can be deleted.

var // declares a global variable b=2 that cannot be deleted ;  // Create a Global object A property that can be deleted this. c=3;   // ditto, you can delete delete A;   // returns false delete b;   // returns True delete C;   // returns True

JavaScript authoritative Guide (6th edition) Learning Note one

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.