JavaScript Basic Focus of Must See

  • 2021-07-02 23:14:19
  • OfStack

Contact with JavaScript this language is a long time, but never systematically to understand such a language. Now just graduated and some reasons do not want to work under the circumstances of the system to understand 1 under such a language, but also want to develop the habit of writing a blog through such a language, because I think this is a very sacred and glorious thing for programmers.

1.1 Background

I believe many beginners forget or confuse the official name of JavaScript: ECMAScript. On June 17, 2015, the official version of ECMAScript 6 was released, namely ECMAScript 2015.

1.2 Syntax

General syntax ellipsis

Emphasize:

1. Raw values and objects: Raw values include Boolean values, numbers, strings, null, and undefined. The other values are objects. The main difference between the two is the way they are compared: Each object has an identity of only 1 and is only equal to itself.


var obj1={};
var obj2={};
alert(obj1 === obj2);

//false

alert(obj1===obj1);

//true

var prim1=123;
var prim2=123;
alert(prim1===prim2);

//true

2. Use typeof and instanceof to classify values.

typeof

操作数 结  果
undefined 'undefined'
null   object
布尔值 boolean
数字 number
字符串    string
函数 function
所有其他的常规值 object
引擎创建的值 JS引擎可以被允许创建1些值,且typeof的结果可以返回任意字符串
   

3. Boolean:

False values: undefined, null, false,-0, NaN, ''

2-yuan logic operator: The 2-yuan logic operator in JavaScript is short-circuited. If the first operand is sufficient to determine the result, the second operand is not evaluated. And ( & & ): If the first operand is a false value, it is returned. Or (): If the first operand is true, return it.

4. IIFE:

Introduce a new scope. Effect: Removes unintentional sharing caused by closures (functions and variables in the surrounding scope to which it is connected).

Example:


var result=[];
for(var i=0;i<5;i++)
{
result.push(function(){return i;});// ( 1 ) 
}
console.log(result[1]()); //5  (not 1)
console.log(result[3]()); //5  (not 3)

The return value of this line marked (1) is always the current value of i, not the value when the function was created. At the end of the loop, the value of i is 5, so all functions in the array return this value. If you want the function marked with line (1) to take a snapshot of the current i value, you can use IIFE.


for(var i=0;i<5;i++)
{
 (function (){
  var i2=i; 
  result.push(function(){return i2});     
 }()
) ; 
}

All these are part of the knowledge that has not been noticed or understood before in the process of sorting out, and are written here to supplement the knowledge points.


Related articles: