No1. Syntax and Type
1. Declaration definition
Variable type: var, define variable; let, define local variables in the block domain (scope); const, define read-only constants.
Variable format: Start with a letter, underscore "_" or $ symbol, case sensitive.
Variable assignment: The value of the declared but unassigned variable is undefined when used. Using the undeclared variable directly will throw an exception.
The unassigned variable is calculated: the result is NaN. For example:
var x, y = 1;
console.log(x + y); //The result is NaN, because x has no assignment.
2. Scope
Variable scope: There is no block declaration field before ES6, and variables act on function blocks or globally. For example, the following code enters x as 5.
if (true) {var x = 5;}console.log(x); // 5ES6 variable scope: ES6 supports block scope, but requires the use of let to declare variables. The following code outputs the exception.
if (true) {let y = 5;}console.log(y); // ReferenceError: y is not defined1234Variable float: In a method or global code, when we use variables before life variables, we do not throw exceptions, but return undefined. This is because JavaScript automatically floats the declaration of variables to the front of the function or global. As shown in the following code:
/*** Global variable float*/console.log(x === undefined); // logs "true"var x = 3;/*** Method variable float*/var myvar = "my value";// Print variable myvar result is: undefined(function() {console.log(myvar); // undefinedvar myvar = "local value";})(); The above code is equivalent to the following code: /*** Global variable float*/var x;console.log(x === undefined); // logs "true"x = 3;/*** Method variable float*/var myvar = "my value";(function() {var myvar;console.log(myvar); // undefinedmyvar = "local value";})();Global variables: In the page, the global object is window, so we can access the global variable through window.variable. For example:
version = "1.0.0";console.log(window.version); //Output 1.0.0
No2. Data structure and type
1. Data Type
6 basic types: Boolean (true or false), null (js case sensitive, different from Null and NULL), undefined, Number, String, Symbol (mark unique and immutable)
An object type: object.
object and function: an object as a container of values and a function as an application process.
2. Data conversion
Function: Converting a string to a number can use the parseInt and parseFloat methods.
parseInt: The function signature is parseInt(string, radix), radix is a number of 2-36 that represents the numerical base, such as decimal or hexadecimal. The return result is integer or NaN, for example, the following output results are all 15.
parseInt("0xF", 16);parseInt("F", 16);parseInt("17", 8);parseInt(021, 8);parseInt("015", 10);parseInt(15.99, 10);arseInt("15,123", 10);parseInt("FXX123", 16);parseInt("1111", 2);parseInt("15*3", 10);parseInt("15e2", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseInt("15px", 10);parseFloat: The function signature is parseFloat(string), and the result is a number or NaN. For example:
parseFloat("3.14"); //Return the number parseFloat("314e-2"); //Return the number parseFloat("more non-digit characters"); //Return the number NaN3. Data type textualization
Textured types: Array, Boolean, Floating-point, integers, Object, RegExp, String.
Additional comma cases in Array: ["Lion", , "Angel"], length 3, the value of [1] is undefiend. ['home', , 'school', ], the last comma is omitted so the length is 3. [ , 'home', , 'school'], length 4. ['home', , 'school', , ], length 4.
integer integer: integers can be expressed as decimal, octal, hexadecimal, and binary. For example:
0, 117 and -345 //Decimal 015, 0001 and -0o77 //Octal 0x1123, 0x00111 and -0xF1A7 //Hex 0b11, 0b0011 and -0b11 1234 //Binary floating point number: [(+|-)][digits][.digits][(E|e)[(+|-)]digits]. For example: 3.1415926, -.123456789, -3.1E+12 (3100000000000), .1e-23 (1e-24)
Object: The property acquisition value of an object can be obtained through ". Attribute" or "[Attribute Name]". For example:
var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };console.log(car.manyCars.b); // Jeepconsole.log(car[7]); // MazdaObject attribute: The attribute name can be any string or an empty string, and invalid names can be included in quotes. Complex names cannot be obtained through., but can be obtained through []. For example:
var unusualPropertyNames = {"": "An empty string","!": "Bang!"}console.log(unusualPropertyNames.""); // SyntaxError: Unexpected stringconsole.log(unusualPropertyNames[""]); // An empty stringconsole.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !console.log(unusualPropertyNames["!"]); // Bang!Transformation characters: The following string output results contain double quotes because the transformation symbol "/"" is used.
var quote = "He read /"The Cremation of Sam McGee/" by RW Service.";console.log(quote);//Output: He read "The Cremation of Sam McGee" by RW Service.1.
String line breaking method: add "/" directly at the end of the string, as shown in the following code:
var str = "this string /is broken /cross multiple/lines."console.log(str); // this string is broken across multiplelines.
No3. Control flow and error handling
1. Block expression
Function: Block expressions are generally used to control flows, such as if, for, and while. In the following code, {x++;} is a block declaration.
while (x < 10) {x++;}There was no block domain scope before ES6: Before ES6, variables defined in blocks were actually included in methods or globally, and the influence of variables exceeded the scope of the block scope. For example, the final execution result of the following code is 2, because the variables declared in the block act on the method.
var x = 1;{var x = 2;}console.log(x); // outputs 2After ES6, there is a block domain range: In ES6, we can change the block domain declaration var to let, so that the variable only scopes the block range.
2. Logical judgment
The special values judged as false: false, undefined, null, 0, NaN, "".
Simple boolean and object Boolean types: There is a difference between false and true of simple boolean types and false and true of object Boolean types, and the two are not equal. As in the following example:
var b = new Boolean(false);if (b) // Return trueif (b == true) // Return false
No4. Exception handling
1.Exception type
Exception throw syntax: The exception throwing can be of any type. As shown below.
throw "Error2"; // String type throw 42; // Number type throw true; // Boolean type throw {toString: function() { return "I'm an object!"; } }; // Object typeCustom exception:
// Create an object type UserExceptionfunction UserException(message) {this.message = message;this.name = "UserException";}//Rewrite the toString method to directly obtain useful information when throwing an exception UserException.prototype.toString = function() {return this.name + ': "' + this.message + '"';}// Create an object entity and throw it throw new UserException("Value too high");2. Syntax
Keywords: Use try{}catch(e){} finally{} syntax, similar to C# syntax.
Finally return value: If finally adds a return statement, no matter what the entire try.catch returns, the return value is finally return. As shown below:
function f() { try { console.log(0); throw "bogus"; } catch(e) { console.log(1); return true; // The return statement is paused until finally execution is completed console.log(2); // Code that will not be executed} finally { console.log(3); return false; // Overwrite the return console.log(4); // Code that will not be executed} // "return false" is executed now console.log(5); // not reachable}f(); // Output 0, 1, 3; Return falseFinally swallowing exception: if finally there is a return and there is a throw exception in the catch. The throw exception will not be caught because it has been covered by the finally return. The following code looks like:
function f() { try { throw "bogus"; } catch(e) { console.log('caught inner "bogus"'); throw e; // The throw statement is paused until finally execution is completed} finally { return false; // Overwrite the throw statement in try.catch} // "return false"}try { f();} catch(e) { // It will not be executed here because the throw in the catch has been overwritten by the return statement in finally console.log('caught outer "bogus"');}// Output// catch inner "bogus"System Error object: We can directly use Error{name, message} object, for example: throw (new Error('The message'));
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.