JavaScript interview Development Common knowledge points summary _javascript skills

Source: Internet
Author: User
Tags error handling exception handling throw exception variable scope

No1. Grammar and type
1. Declaration definition
Variable type: var, define variable; Let, define block domain (scope) local variable; const, define read-only constants.
Variable format: Starts with a letter, an underscore "_", or a $ symbol, which is case-sensitive.
Variable assignment: A variable that is declared but not assigned is undefined when the value is used, and the undeclared variable is used directly to throw an exception.
The unassigned variable is evaluated: The result is Nan. For example:
var x, y = 1;
Console.log (x + y); The result is Nan because X is not assigned a value.
2. Scope
Variable Scope: There is no block declaration field before ES6, and the variable acts on a function block or global. For example, the following code enters an X of 5.

 if (true) {
var x = 5;
}
Console.log (x); 5 

ES6 Variable Scope: ES6 support block scope, but need to use let declaration variable. The following code output results throw an exception.

I f (true) {let
y = 5;
}

Variable float: In a method or global code, we do not throw an exception when we use the variable before the life variable, but we return to undefined. This is because JavaScript automatically floats the declaration of a variable to the front of the function or global. As in the following code:

 /**
* global variable floating * *
console.log (x = = = undefined);//Logs "true"
var x = 3;

/**
* Method Variable Float * *
var myvar = "My value";
The print variable myvar result is: Undefined
(function () {
console.log (myvar);//undefined
var 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); Undefined
myvar = "local value";
}) (); 

Global variables: In the page, the Global object is window, so we can access global variables 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 Base types: Boolean (True or FALSE), null (JS case sensitive, and null, NULL is distinguished), undefined, number, String, Symbol (tag unique and immutable)
An object type: objects.
Object and function: objects as containers of values, functions as procedures for the application.
2. Data Conversion
Functions: String conversions to numbers use the parseint and Parsefloat methods.
Parseint: The function is signed as parseint (String, radix), and Radix is a number 2-36 that represents the numeric cardinality, such as decimal or hexadecimal. Returns the result to be an integer or Nan, for example, the output below is 15.

parseint ("0xF");
parseint ("F");
parseint ("8");
parseint (021, 8);
parseint ("015");
parseint (15.99);
Arseint ("15,123");
parseint ("FXX123");
parseint ("1111", 2);
parseint ("15*3");
parseint ("15e2");
parseint ("15px", 10); 

Parsefloat: function is signed as parsefloat (String), and the result is a number or Nan. For example:

Parsefloat ("3.14"); Returns the number
parsefloat ("314e-2");//return number
parsefloat ("more non-digit characters");//Return Nan 

3. Text of data type
type of text: Array, Boolean, floating-point, integers, Object, RegExp, String.
Extra comma condition in array: ["Lion", "Angel"], length 3,[1] value is undefiend. [' Home ',, ' school ',], the last comma omitted so the length is 3. [, ' Home ',, ' school '], length 4. [' Home ',, ' school ',,], length 4.
Integer integers: Integers can be expressed as decimal, octal, 16, binary. For example:

 0, 117 and-345//Decimal
015, 0001 and-0o77//Eight 0x1123, 0x00111 and-0xf1a7
//16 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 fetch value of an object can be obtained by either ". Property" or "[Property name]". For example:

 var car = {manycars: {a: "Saab", "B": "Jeep"}, 7: "Mazda"};
Console.log (car.manycars.b); Jeep
Console.log (car[7]);//Mazda 

Object properties: An attribute name can be any string or an empty string, and invalid names can be included in quotes. Complex names cannot pass. Get, but can be obtained by []. For example:

 var unusualpropertynames = {"
": "An empty string",
"!": "bang!"
}
Console.log (unusualpropertynames. ""); syntaxerror:unexpected string
console.log (unusualpropertynames[""]); An empty string
console.log (unusualpropertynames.!);//syntaxerror:unexpected token!
Console.log (unusualpropertynames["!"]); bang! 

Conversion character: The string output below contains double quotes because the symbol "\" is used.

 var quote = "He read \" The cremation's Sam mcgee\ "by R.W. Service.";
Console.log (quote);
Output: He read "The cremation of Sam McGee" by R.W. Service.1. 

String Wrapping method: Add "\" directly at the end of the string line, as shown in the following code:

 var str = "This string \ is
broken \
across 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 statement.

 while (X < ten) {
x + +;
} 

ES6: Before ES6, the variables defined in the block are actually contained in the method or global, and the effect of the variable is beyond the scope of the block scope. For example, the following code ultimately executes 2 because the variables declared in the Block Act on the method.

 var x = 1;
{
var x = 2;
}
Console.log (x); Outputs 2 

After ES6, there is a domain scope: in ES6, we can change the block domain declaration var to let, so that the variable only scope block range.

2. Logical Judgment
Special values that are judged to be false: false, undefined, null, 0, NaN, "".
Simple Boolean and Object Boolean types: The simple Boolean type false and True are different from the object Boolean type false and true, and the two are not equal. As in the following example:

 var B = new Boolean (false);
if (b)//return True
if (b = = true)//return False 

No4. Exception Handling
1. Exception type
Throw exception syntax: Throw an exception can be any type. as shown below.

 Throw "Error2"; String type
throw 42;//numeric type
throw true;//Boolean type
throw {tostring:function () {return "I ' m a object!";};//Pair Like type 

Custom Exceptions:

 Creates an object type Userexception
function userexception (message) {
this.message = message;
THIS.name = "Userexception";
}

Override the ToString method to get useful information directly 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. Grammar
Keyword: use Try{}catch (e) {}finally{} syntax, similar to C # syntax.
Finally return value: If Finaly adds a return statement, then whatever the entire Try.catch returns, the returned value is finally a. 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 completes
    Console.log (2);//The code that will not execute is
  finally {
    console.log (3);
    return false; Overwrite Try.catch return
    console.log (4);//Will not execute code
  }
  //"returned false" is executed now 
  Console.log (5);// Not reachable
}
f ();//Output 0, 1, 3; return false 

Finally annexation exception: If finally there is return and there is a throw exception in the catch. The throw exception is not captured because it is already covered by the finally return. As shown in the following code:

 function f () {
  try {
    throw ' bogus ';
  } catch (e) {
    console.log (' caught inner ' bogus ');
    Throw e; The throw statement was paused until finally execution completed
  } finally {return
    false;//overwrite Try.catch throw statement
  }
  //"return FALSE" has been executed
}

The try {
  f ();
} catch (e) {
  //is not executed here because the throw in the catch has been overwritten by the finally return statement
  Console.log (' Caught Outer "bogus");
Output
//caught inner "bogus" 

System Error object: We can directly use Error{name, message} object, for example: throw (' the message ');

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.