JavaScript Basics Getting Started Tutorial (iii)

Source: Internet
Author: User
Tags arithmetic operators define local

Description

The previous two blogs introduce the variable types, identifiers, and so on in the basics of JS. This blog mainly talks about expressions and operators.

Original expression

The original expression is the smallest in the expression, cannot be in the split expression, generally refers to the variable, constant direct amount, the keyword (true, false, NULL, this), it is important to note that undefined is a global variable, not a keyword.

Initialization expressions for objects and arrays

An array initialization expression is a square bracket in which the elements in the array are placed, separated by commas between the elements. An element can be an expression, an array, an empty.

1 []    // an empty array with no elements in the array 2 [1+2,3+4]    // An array with two elements, the first is 3, The second one is 7.  3 [[1,2,3],[4,5,6],[7,8,9,]]    // The elements in the array can also be arrays.  4 [1,,,, 5]    // The element in the array can be empty, when the empty space fills the value undefined. 

After executing the Var a=[1,,,, 5] This statement, it behaves as shown in Chrome:

Object initialization expressions and arrays of initialization expressions are very similar.

1 var stu={name: "Zhang San", age:15};    // defines an object that has two properties. 

The above line initializes the object with an expression similar to the following array, and in fact JS does support this way of accessing properties in the object.

1 stu["name"]= "Zhang San"; 2 stu["age"]=15;

If you have learned Java then the above-mentioned Var stu={name: "Zhang San", age:15}, can be easily written in the following Java code:

1 classstudent{2 String name;3     intAge ;4 5Student (String name,intAge ) {6          This. name=name;7          This. age=Age ;8     }9 }Ten  OneStudent stu=NewStudent ("Zhang San", 15);

However, JS can perfectly support multi-layered internal classes, while Java has a lot of restrictions on internal classes.

Property-Access Expression

In JS, property access has ①expression.identifier, ②expression[expression] both ways. The first of these, although convenient, can only be used for objects, and the attributes required for access (identifier) cannot be keywords, cannot contain spaces, and cannot be numbers. The second type of access not only supports the object but also the array, and there is not the first number of restrictions for the object to access its own properties.

1 varO={x:1, Y:{z:3}};2 varA=[o, 4, [5, 6]];3O.x//1: Access O's X attribute4O.y.z//3: Z property of y to access o5o["X"]//1: Access O's X attribute6A[1]//4: Access an element with an index of 17a[2]["1"]//6: Access to the element with index 1 in a[2]8a[0].x//1: By accessing a[0] Get O, then access O's X attribute

Operator

JS supported operators as shown, students who have learned other programming have a pretty eye on it. Only the parts that need attention are explained here. In the basic arithmetic operators (subtraction and redundancy) only the addition is relatively difficult, because sometimes it is an addition, and sometimes it is a string connector, the specific usage can be summarized as only the + operator on both sides of the number is the addition, more detailed usage please refer to this blog. In addition, we need to talk about the remainder operator, whose operation result is the same as the left operand, i.e. 5%1=1, -5%1=-1.

In the bit operation of JS, there will be an implicit conversion process. By default, the numbers in JS are stored as 64-bit floating-point number formats, which are converted to 32-bit integers at the time of the in-place operation, so the implicit conversion truncates the fractional part and the portion of the integer more than 32 bits can represent. Also Nan, infinity, and-infinity are converted to 0.

Refer to the first article in this series for the equals (= =) operator and the strict equals (= = =) operator.

In operator

The in operator expects that its left operand is a string or can be converted to a string, and expects its right operand to be an object. The entire in expression returns true if the right-hand object has a property called the left operand.

1 varPoint={x:1, Y:1};2"X"inchPoint//true: There is a property named X in the object3"Z"inchPoint//false: A property named Z does not exist in the object4"ToString"inchPoint//true: The object inherits the ToString () method5 6 varData=[7, 8, 9];7"0"inchData//true: The number contains the element labeled 081inchData//true: Numbers are converted to strings93inchData//false: No element with index 3
&& | |

Students who have studied languages such as C and Java should know that the two operators have a feature that is short-circuiting, which is also present in JS, and habitually uses this feature to provide default values for functions.

1 function copy (o, p) {2     O.P = P | |  {}; 3 }

In the above code, if the copy function is called, the P parameter has a value, then p is copied to O.P, otherwise the default value {} is copied to O.P.

Expression evaluation

In JS there is a function called eval (), although this is a function, but is generally treated as an operator. In general, functions can be aliased, such as Var f=eval, and then the Eval function is called through F. However, it is generally not recommended to alias the Eval function, and the ECMASCRIPT3 standard even directly prohibits the eval function from being aliased, so it is almost treated as an operator. This is because this involves a problem with code optimization, and allowing an alias to the Eval function seriously affects the code's optimization process.

The above talk about the characteristics of Eval, now for its role. This function has only one parameter and is generally a string. If the passed argument is a string, the JS interpreter will execute the string as a JS code, and its execution environment will directly invoke the context of the Eval function (explained in detail below), and if its incoming argument is not a string, the Eval function returns the parameter value directly.

1 var global=1; 2 var function (ele) {3     var local = 2; 4     eval (ele); 5     Console.log (where); 6 }7 foo ("Console.log (global); Console.log (local); var where= ' I am here! ';" ); 8 Console.log (where);

The result of the above line of code (in Chrome) is as follows:

According to the eval execution environment above, the context of the eval is called directly, and in the result of the display is the context of line 8th, so that eval can naturally access the global and local variables, But its defined where variable can only be accessed by code inside the Foo function and after line 8th, so that the 9th line of code executes correctly, and the 12th line of code throws an exception.

I've talked about ECMASCRIPT3. The alias for Eval is forbidden, but the implementation of most of the interpreters in reality is not so, and the subsequent ECMASCRIPT5 standard allows aliases to be invoked, but the behavior is regulated. When Eval is called by an alias, its running environment is always the global environment, not related to its caller environment, meaning that indirect calls (alias calls) cannot read, write, or define local variables.

1 varGeval =eval;2 varx = "global", y = "global";3 functionf () {4     varx= "Local";5Eval ("x + = ' changed ';"));6     returnx;7 }8 functiong () {9     vary= "Local";TenGeval ("Y + = ' changed ';")); One     returny; A } - Console.log (f (), x); -Console.log (g (), y);

The result of the above code execution (in Chrome) is:

JavaScript Basics Getting Started Tutorial (iii)

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.