JavaScript Summary 1

Source: Internet
Author: User

One, JavaScript variable type, declaration, scope 1.1 number

Decimals and integers are called number, and hexadecimal is represented by the beginning of 0x or 0X. When infinity, use infinity (try 9/0), other non-numbers with Nan (not-a-number, try 9/' a '); About Nan, he does not equal any value, including himself. Nan! = Nan or Nan!==nan returns True. It should be noted that, due to the accuracy of the decimal point, if the RMB is involved, the unit is best to use points.

1.2 Strings String

Enclosed in double quotation marks, the string has special characters that can be used with the \ escape character. There are many function functions that can be used.

1.3 BOOL Boolean

The type has only two values of Ture/false. In JS, Undefined,null,0,-0,nan, and the empty string "" are equal to false.

1.4 Null

He is used to describe a null value, which needs to be noted: The result of typeof null is object.

1.5 undefined

A value of a variable that indicates that the variable is not initialized, or that there is no such property in the object, and that the function does not return a value.

NULL = = undefined//return ture

NULL = = undefined//return False

1.6 Object

Contains the object {}, array [], function Functions () {}, which can be used. or [] to access the properties within it.

2 Variable declaration and scope

All type variables are declared with Var and can be declared one or more at a time. JS is a function scope, unlike java,c#, which is a block-level scope. A variable declared inside a function is a local variable and must be var. Otherwise it becomes a global variable, declaring a global variable The fact is that a property of a Window object is defined (Var a can be understood as WINDOW.A), and the local variable takes precedence over the global variable with the same name. In the variable declaration, JS has a feature called Declaration advance (hoisting), that is, regardless of where the variable is declared, he will advance to the front of the scope where he is located. Refer to the online section of the code

if (! (" A "in window) {var a =  1;} alert (a);

He's going to pop undefined, why not 1? Because of the hoisting attribute, Var A is declared ahead of time, as an unassigned global variable, when window has a property, "a" in window returns True, a = 1 is not executed, and this code is equivalent to

var A; if (! (" A "in window)) {a =  1;} alert (a);

Second, operator

Some of the commonly used operators are similar, and the more special ones are:

1 =,== and = = =

Their definition is somewhat different, = = = the strict equality operator (strict equality), called identity , = = is called equality , = is called assignment .

= = = When compared, the values are compared first, and no type conversions are made, and the values are equal and the types are equal, and true is returned. For reference values, you must point to the same object for equality. Null = = undefined returns false,null = = undefined returns true;

2 in and instanceof

In determines whether the left string value is the property of the right object

Instanceof judge the object on the left is not the right class

var test = {  x:1} "x" in test//true "y" in test//falsevar test_date = new Date (); Test_date instanceof Date;//truetes T_date instanceof object;//truetest_date instanceof Regexp;//false
3 && | |

Not only do they return a Boolean value!

&& logic calculates the value from left to right once, as long as there is a false in the period, then the right side is not calculated, and returns false directly.

There is a special usage called Short circuit, which is a technique to prevent type errors.

|| Logic or he calculates the value from left to right once, as long as there is a true, then the right side will not be calculated, directly return true.

He has a special usage. You can provide default values for parameters.

/* Use of short circuit * prevents Test from being empty, resulting in error when x value is taken */var test = {  X:1}var y = Test && test.x;//y = 1;/* provides default * if data is empty, set to XXXX by default */function TEST_FN (data) {  data = Data | | "XXXX";  /   * Other actions */   }

Share a mind map to find online:

Third, the statement

Common loops, conditions, jumps, exception-handling statements are similar to Java. What's special is that for for-in loops, he loops through the enumerable properties of the object. Condition If-else,switch-case. One trick is that when the condition is enumerable, say 1 for Monday, 2 for Tuesday ... In this case, the enumeration value is cached in an array, and the performance of the index is better than the conditional judgment.

For with, he temporarily changed the scope chain, preferably not used. Debugger is used to generate a breakpoint.

Use Strict is an instruction that can only appear at the beginning of the script code or at the beginning of the function body, indicating that the following code executes in strict mode. The distinction between strict and non-strict modes is referenced in section 5.8 of the JavaScript authoritative guide.

JavaScript Summary 1

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.