Former 2017/5 JavaScript Foundation 5 - Statements, Strict Patterns

Keywords: Attribute

Statement: A javascrit program consists of statements that follow specific grammatical rules. For example: if statement, while statement, with statement

Block block

Block statements are often used to combine 0 to more than one statement. Block statements are defined with a pair of curly braces

1. Grammar:

{
   Statement 1; 
   Statement 2;
}

Usually used in conjunction with if and while

if(true){
  console.log("hi")
}

2. No block-level scope

1) After the for loop is completed, it can also be accessed outside.

for(var i =0 ; i<10 ; i++){  //i Although the declaration is inside for, it can also be accessed outside.
   var str = "hi";
   console.log(str);
}
var i=0;
for( ; i<10;i++){
  var str = "hi";
  console.log(str);
}
//This style of writing serves the same purpose as above.

2) Functional scope

function foo(){
  var a = 1;
  console.log(a); //1
}

foo();
console.log( typeof a) ; //undefined not accessible

3. Declarations

1)var  a = b =1;

When var a = b =1 is used, it does create two variables and assign one to 1, but B implicitly creates the global variable.

function foo(){
  var a = b = 1;  
} 

foo();
console.log(typeof a) ; //"undefined"
consloe.log(typeof b) ; // Number B is a global variable

2) Declare multiple variables in a statement

var a =1 ,b =1;

II. try - catch statement

1. try-catch form

  1. try  -- catch
  2. try -- finally
  3. try --catch -- finally

try must be followed by a catch() or finally

try{
  throw "test";
}catch(ex){  //Execute when an exception is thrown
  console.log(ex); // test
}finally{  //It will be executed in the end.
  console.log('finally')
}

2. try-catch nesting

1) Nested 1

Try2 throws an exception "opps" that has no catch in try2, so it jumps out to find the nearest catch, but executes last first.

try{
/************try2***************/
  try{
      throw new Error("opps");
  }finally{
      console.log("finally");
  }
/*****************************/

}catch(ex){
  console.error("outer" , ex.message);
}

finally

outer opps

2) Nested 2

try{
/************try2****************/
  try{
    throw new Error("oops");
  }catch(ex){
    console.error("inner",ex.message);
  }finally{
    console.log("finally)
  }

/************try2****************/

}catch(ex){
  console.error("outer" , ex.message);
}

Throw an exception in it and deal with it directly.

inner  oops

finally

If the exception inside is not handled and thrown, the internal final statement is handled before the exception outside is handled.

2. Function switch, loop

1,function

1) Function declaration:

function fd(){
  //do sth
  return
}

2) Functional expression:

var fe = function(){
  //do sth
 
} 

3) Distinction

Function declarations are pre-processed, and functions are pre-processed.

//Correct
fd()
function fd(){
  //do sth
  return
}

Function expressions cannot be called before declaration

//error
fe(); TypeError
var fe = function(){
  //do sth
 
} 

2. For... in statement

var p;
var obj = { x: 1, y:2}

for(p in obj){}
var obj = {a:1,b:2,c:3}
for( var prop in obj){
console.log("obj." + prop + "=" +obj[prop])
}

/*********Output************/
obj.a=1
obj.b=2
obj.c=3

Loop traversal of object properties

  1. The order is uncertain. Specific order depends on Engine Implementation
  2. Belong to descriptor, eneumerable will not appear when false
  3. for in object attributes are affected by the prototype chain. If the prototype on the obj prototype chain has other attributes, eneumerable will appear when true.

3,switch  

var val = 2;
switch(val){
  case 1:
   console.log(1); break;
  case 2:
   console.log(2); break;
  default:
   console.log(0); break;
}

Output 2

You can also execute a statement with multiple conditions

var val = 2;
switch(val){
  case 1:
  case 2:
   console.log(1,2); break;
  default:
   console.log(0); break;
}

 3,with

with statements can modify the current scope (not recommended)

with({x:1}){
  console.log(x);
}

//In the with statement, you can use X to output 1 directly, without object.x.

3. Strict Model

Strict mode is a special execution mode, which fixes some language deficiencies, provides stronger error checking, and enhances security.

1. Usage

function func(){
  'use strict'; //Strict model
}

Or you can use'use strict'before js files, the whole file strict mode

2. Requirements under Strict Mode

  1. Not allowed to use with
  2. Undeclared variables are not allowed to be assigned. Error-reporting Reference Error
  3. Static copy of arguments to parameters
  4. delete parameter, function name error syntaxError
  5. delete non-configurable property: configurable: false error type Error
  6. Object literal duplicate attribute name errors var obj = {x:1, x:2} error syntax Error (syntax error)
  7. Forbid console. log (0123) - - 83 in normal mode of octal independent variable, and report syntax Error in strict mode.
  8. Eval, arguments become keywords and cannot be used as variables, function names, syntaxError (syntax error)
  9. eval becomes an independent scope

Posted by freephoneid on Sun, 30 Jun 2019 19:17:31 -0700