Some basic JavaScript knowledge

Source: Internet
Author: User
Tags acer

I used to despise JavaScript, and now the project is used. I still want to learn, because the network is growing faster and faster, the future of JavaScript should be good.

1: No multi-dimensional array concept in Javascript

VaR AA = new array ();
AA [0] = 1;
AA ["TT"] = [0, 1]; // JavaScript uses a negative number, floating point number, (or boolean type, object, other values ), javascript will convert it into a string and use the generated string as the object's property name, instead of defining a new array element.
AA ["BB"] = [2, 3];
// Traverses all attributes in the array, not just the array elements
For (var I in aa ){
Document. Write (AA [I]);
}
Document. Write (AA. Length );
Document. Write (AA ["TT"]. Length );

VaR myarray = new array ();
For (VAR I = 0; I <10; I ++ ){
Myarray [I] = new array ();
Myarray [I] [0] = math. Floor (math. Random () * 10 );
Myarray [I] [1] = math. Floor (math. Random () * 10 );
Myarray [I] [2] = math. Floor (math. Random () * 10 );
Myarray [I] [3] = math. Floor (math. Random () * 10 );
Myarray [I] [4] = math. Floor (math. Random () * 10 );
Myarray [I] [5] = math. Floor (math. Random () * 10 );
Myarray [I] [6] = math. Floor (math. Random () * 10 );
Myarray [I] [7] = math. Floor (math. Random () * 10 );
Myarray [I] [8] = math. Floor (math. Random () * 10 );
}
Myarray [10.2] = 11;
Myarray [9] [9] = 22;

Myarray. Sort (function (x, y ){
Return (X [0] = Y [0])? (X [4] = Y [4])? (X [8]-y [8]) :( X [4]-y [4]) :( X [2]-y [2])
});
VaR COUNT = 0;
For (VAR I = 0; I <myarray. length; I ++)
{
Document. Write (myarray [I]. Join (",") + "<br/> ");
For (var j = 0; j <myarray [I]. length; j ++ ){
Document. Write (myarray [I] [J] + "H" + "</BR> ");
Count ++;
}
}
Document. Write (myarray. Length );//
Document. Write (myarray [0]. Length );
Document. Write (count );

2: javascript has a full range of operators, including arithmetic, logic, bit, and value assignment operators. There are also some other operators. 

Calculation logic bit assignment Miscellaneous
Description symbol
Negative value-non-logical! Bitwise inversion ~ Assignment = delete Delete
Increment ++ less than <shift left by bit <operation value assignment op = typeof operator typeof
Decrease -- greater than> shift right by bit> void operator void
Multiplication * less than or equal to <= shifts right without symbol>
Division/greater than or equal to> = bitwise AND &
Modulo operation % equals = bitwise XOR or ^
Addition + is not equal! = Bitwise OR |
Subtraction-logic and &&
Logic or |
Condition (ternary operator )? :
Comma,
Constant equal =
Not constant! =

Operator precedence
Operators in JavaScript are evaluated in a specific order. This order is the operator priority. The following table lists these operators at the highest to lowest priority. Operators in the same row are evaluated from left to right.

Operator description
. [] () Field access, array subscript, and function call
++ ---~ ! Typeof new void Delete unary operator, returned data type, object creation, undefined Value
*/% Multiplication, division, modulo
+-+ Addition, subtraction, and string connection
<>>>> Shift
<=> = Less than, less than or equal to, greater than, or greater than or equal
=! ===! = Equal to, not equal to, constant, non-Constant
& Bitwise AND
^ Bitwise OR
| By bit or
& Logic and
| Logical or
? : Condition
= Op = value assignment and Operation Value assignment
, Multiple evaluate

Parentheses can be used to change the order of evaluation. The expressions in parentheses should be evaluated before they are used for the rest of the statement.

An operator with a higher priority is evaluated before an operator with a lower priority. For example:

Z = 78*(96 + 3 + 45)

There are five operators in this expression: =, *, (), +, and +. Based on the priority, they are evaluated in the following order: (), *, +, +, =.

Evaluate the expressions in parentheses: There are two addition operators, which have the same priority: 96 and 3, and then add their sum and 45, the result is 144.
Then there is the multiplication operation: multiply 78 and 144, and the result is 11232.
Finally, the value assignment operation: Assign 11232 to Z.

3: brackets in Javascript have four meanings.


Semantic 1: Organizes compound statements, which is the most common

?
12345678 If(Condition ){//...}Else {//...}For(){//...}



Semantics 2: Object Direct Volume Declaration

?
1234 VaR OBJ = {Name:'Jack',Age: 23};

It is a value assignment statement, where {Name: 'jack', age: 23} is an expression.



Semantics 3: declaring the direct quantity of a function or function

?
1234567 Function F1 (){//...} VaR F2 =Function(){//...}

The difference between F1 and F2 is that the former is in the syntax interpretation period and the latter is in the runtime period. The difference is: If you call this functionCodeAfter the function is defined, there is no difference. If the code that calls the function is still available before the function is defined, F1 can still be called, and F2 will report an error, prompting that F2 is not defined.



Syntax 4: syntax symbols for structured exception handling

?
1234567 Try {//...}Catch(Ex ){//...} Finally {//...}

Braces and matching statements (Semantic 1) Is different. If there is only one statement in braces, the braces such as if/else/for can be omitted, but try/catch/finally cannot be ignored.



The following code is even n long

?
12 Function(){}()// Immediate execution of anonymous functions. syntax analysis reports{}. Constructor// Get the constructor of the direct object quantity. An error is reported during the syntax analysis period.

 


What is puzzling is why []. constructor does not report an error when writing this code. One is a constructor that wants to get the direct amount of objects, and the other is a constructor that gets the direct amount of arrays.

Of course, no error will be reported if a variable is added for receiving.

VaR c = {}. constructor;

The same situation is shown in figure

VaR fn = function () {} (), and no error is reported.

In fact, JS statements take precedence. That is, {} is interpreted as a composite statement block (Semantic 1) Instead of the Direct Volume of objects (Semantic 2) Or declare a function (Semantic 3.

Function () {} (), braces are understood as compound statements. The syntax of the previously declared function () is incomplete, which leads to errors in the syntax analysis period.
{}. Constructor. Braces are considered as compound statements. Braces are followed by dot operators. If there are no reasonable objects before the dot operators, an error is also reported.

Solution: Add a forced operator ()
(Function () {}) (), (function () {}); // force it to be understood as a function (Semantic 3), "Function ()" indicates that the function is executed, that is, it is executed immediately after the declaration.
({}). Constructor // ({}) force the braces to be understood as the object direct quantity (Semantic 2), "Object. xx" indicates to get the object's members. The dot operator after it can be executed normally.

 

 

Previously, it was reprinted from other places, and gradually increased with the use process.

3: JSON usage

Convert a JSON object to a string:
VaR JS string = JSON. stringify (JSON object );

Multi-dimensional JSON object:VaR A = [{A: 123, B: 234, C: "123 "},{A: 123, B: 234, C: "123"}];
JSON object of a single dimension:VaR A = {A: 123, B: 234, C: "123 "};

 

Javascript JSON reference variable

First, define public variables before defining JSON, such:
VaR AC = "values of A and C ";
VaR JSON = {A: AC, B: "B", C: AC };

Second, assign values after definition, as shown in figure
VaR JSON = {A: "values of A and C", B: "B "};
JSON. c = JSON.;
Or
VaR JSON = {A: "values of A and C", B: "B", C: NULL };
JSON. c = JSON.;

Dynamically add JSON Variables

VaR DATA = {}
For (VAR I = 1; I <4; I ++)
Data ["G" + I] = I;

4: traverse array and Object Attributes

The array of JavaScript has the following creation methods:

 
//Create ArrayVaRComputers =NewArray (); computers [0] = "Dell"; Computers [1] = "Acer";//Create a condensed ArrayVaRComputers =NewArray ("Dell", "Acer");//Create literal ArrayVaRComputers = ["Dell", "Acer"];

You can traverse the array as follows:

 
//Traverse ArraysFor(VaRI = 0; I <computers. length; I ++) {Alert (computers [I]);}

Javascript objects are wrapped by a pair of curly braces. See the following example:

//Create an objectVaRComputer = {brand: "Acer", price: 100.50, ID: 6};//You can also createVaRComputer =NewObject (); computer. Brand= "Acer"; Computer. Price= 100.50; Computer. ID= 6;//You can also define a constructor.FunctionComputer (brand, price, ID ){This. Brand =Brand;This. Price =Price;This. ID =ID ;}VaRComputer1 =NewComputer ("Acer", 100.50, 6 );

The above brand is the computer property name, and the value of this property is Acer. Similarly, the computer object has two attributes: price and ID.

You can obtain the attributes of an object in either of the following ways:

 
//Access PropertyVaRV1 =Computer. Brand;VaRV2 = computer ["brand"];

Traverse Object Attributes

 
//Traverse Object AttributesFor(VaRIInComputer) {alert (I+ ":" +Computer [I]);}
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.