The ambiguity of the JavaScript operator

Source: Internet
Author: User

Recently through a colleague's introduction and read a book called "JavaScript Language Essence and programming practice," a lot of details about JS, such as I would like to talk about the two of the semantics of the operator, then the question, what is two righteousness, the literal understanding of course there are two meanings, So what is the ambiguity of the operator? The two semantics of an operator is that the operator represents an operation that is not unique, such as the following example

1.var str = ' Hello ' + ' world ';//the ' + ' operator represents a connection to a string

2.var n=10;

var n2=+n;//here the ' + ' operator represents a positive value

3.var n=3+4;//here ' + ' represents the sum operation

Why does an operator have multiple meanings, for the simple reason that the arithmetic processing in JS depends on the data type, then let's look at an example

Here if you want to change the width of the border of this picture you might first think of the following code

var e1=document.getelementsbytag (' img ');

e1.style.borderwidth+=10;

Then the problem comes again e1.style.borderWidth in JS will back a unit of the value "1px", and this value and 10 for ' + ' Operation JS will return to you a concatenation of the string ' 1px10 ', JS is not recognized this type, so its value will be ' 1px '

Let's take a look at the operators we often encounter when we write code ","

Directly on the example

Example one: A= (n/a);

Example two: a=1,2,3;

Can you guess the result, the example returns the value 3 example two returns 1, because ', ' can be a syntax delimiter and can be an operator caused by the example of a three single Zhi expression continuous operation, the result value is the last expression is 3, In example two, because there is no parentheses to enforce the priority of the operation, so the default priority will be calculated, so the second return is 1 then we look at the following

var a=1,2,3;

This line of code is much like the above example, but the result of this line of code is that it will produce an error, because in this case ', ' is interpreted as a delimiter for VAR declaring a variable instead of a continuous operator, the statement var requires ', ' to split multiple identifiers, and 2,3 is clearly not a valid identifier. The lesson here is that the Var declaration makes the continuous continuous operation expression A continuous declaration statement.

Let's take a brief look at the ambiguity of {}

{} has four kinds of effects:

Example one: representing a compound statement

mylabel:{//Compound statement after identifying the label

//....}

We know that the statement '; ' can be omitted before the curly brace at the end of the compound statement so we can write our code like this

{A-i}

Example two: Declaring the object's direct amount

var obj={

Vi:1,

V2:2,

V3:3

}

Because the whole is an assignment statement, the direct volume declaration part is actually an expression:

V1:1,

V2:2,

V3:3

Note that they are all an expression here, so we have two ways to turn this expression into a statement, as explained earlier.

The first type is '; ' notation

V1:1,

V2:2,

V3};

The second is the compound statement notation

V1:1,

V2:2,

V3;3

There are many more like ' + ', ', ', ' () ' that have the ambiguity of the operator

We're not going to give a detailed example of a brief introduction.

: Declaration label Declaration Switch Branch

[] Array subscript array Direct Volume declaration

{} function Direct volume declaration object Direct Volume Declaration compound statement

; Empty statement Statement delimiter


The ambiguity of the JavaScript operator

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.