The difference is that JavaScript is written differently.

Source: Internet
Author: User
The difference is that JavaScript is not the same. Today we will introduce different javascript writing methods. It is very simple. Today we will introduce different javascript writing methods. It is very simple.
1. If the condition is set, method a is executed. If the condition fails, Method B is executed.
We usually write as follows:
Var result; if (isOk) {result = funA ();} else {result = funB ();} can also be expressed as follows:
Var result = isOk? FunA (): funB () 2. When the condition is true, execute a method.
Common method:
If (isOk) {doSomething ();} I prefer to write like this:
IsOk & doSomething (); If a variable is not defined or has no value, give it a default value.
Str = str | "OK"; arr = arr | []; The preceding method is feasible because the return value is not necessarily true or false in js logic operations, it can also be any other value, and 0, "", null, false, undefined, and NaN will be judged as false, and all others will be true. Example:
1 & "OK" // The expression value is string "OK", logically determined to be true1 | "OK" // The expression value is a number 1, logically determined to be truenull | [] // The expression value is an array [], and logically determined to be truenull & [] // The expression value is null, logically determined to be false 3. When multiple condition determination segments are performed
For example, each color corresponds to a value, such as "white", "red", "green", "yellow", "gray ", "blue" corresponds to values 0, 1, 2, 3, 4, 5 respectively.
1) Question 1: Obtain the color based on the Value
Method 1
? Function getColorByVal (val) {var color = ""; if (val = 0) {color = "white";} else if (val = 1) {color = "red";} else if (val = 2) {color = "green";} else if (val = 3) {color = "yellow ";} else if (val = 4) {color = "gray";} else if (val = 5) {color = "blue";} return color ;}
Implementation Method 2

Function getColorByVal (val) {var color; switch (val) {case 0: color = "white"; case 1: color = "red"; break; case 2: color = "green"; break; case 3: color = "yellow"; break; case 4: color = "gray"; break; case 5: color = "blue "; break;} return color ;} Method 3
? Function getColorByVal (val) {return ["white", "red", "green", "yellow", "gray", "blue"] [val];}
Call: var color = getColorByVal (2 );
There is no difference between method 1 and method 2, but method 2 is a little better, while method 3 is eye-catching and completes the function. However, some people may say that the color value is just an array subscript, so let's try again:
2) Question 2: obtain the value based on the color

You can use the if or switch statement, but the following two methods are provided:
Method 1:

Function getValByColor (color) {var colors = ["white", "red", "green", "yellow", "gray", "blue"]; var result; for (var I = colors. length-1; I --;) {if (colors [I] = color) {result = I; break;} return result ;} Method 2:
Function getValByColor (color) {return {"white": 0, "red": 1, "green": 2, "yellow": 3, "gray": 4, "blue": 5} [color];} call: var val = getValByColor ("red ");
The comparison is obvious. method 2 is simpler and easier to understand. it cleverly constructs an object and obtains values through attributes, thus avoiding tedious judgment.
4. Exchange the values of two variables
This is usually implemented as follows:
Var temp = 0, a = 5, B = 10; temp = a; a = B; B = temp; but it can be more clever:
Var a = 5, B = 10; a = [B, B = a] [0]; a = [B, B = a] [0] execution process: first, two expressions B and B = a in the number group are executed to assign 5 to B and generate an array []. Then a = [] [0], that is, a = 10, in this way, the exchange is completed, but the unknown array is also used, but it seems that the third variable is not used, but this is not recommended. After all, the first method is easier to understand. 5. Obtain the attributes of an object. var arr = [], I = 0; var colors = {"white": 0, "red": 1, "green": 2, "yellow": 3, "gray": 4, "blue": 5}; for (var key in colors) {arr [I ++] = key;} method 2
Var arr = [], I = 0; var colors = {"white": 0, "red": 1, "green": 2, "yellow": 3, "gray": 4, "blue": 5}; for (arr [I ++] in colors ); the attribute of the colors object (arr = ["white", "red", "green", "yellow", "gray", "blue"]), the second method is not applicable to the for in statement. The for in statement in method 1 obtains an attribute assigned to the key from colors in sequence, but assigns the attribute to arr [I ++] in method 2. This is purely for fun. Enjoying the fun of programming is part of programming ..
Is it interesting to find javascript? If it is interesting, we recommend it.

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.