Summary of JS issues

First, the operator

var a,b=0;
console.log(a);//undefined
console.log(b);//0

, the comma operator evaluates its operands from left to right, returning only the value of the last operand;

 The comma expression is a continuous expression, and its result is the last value

 

Second, parseInt incoming numbers

Why is it like this:

parseInt(0.000008) // >> 0
parseInt(0.0000008) // >> 8

parseInt(arg)will be called first arg.toString().

(0.000008).toString() // "0.000008"
(0.0000008).toString() // "8e-7"
parseInt("0.000008");//0
parseInt("8e-7");//8

 3. Immediately execute the expression

(function (){} ());//Brackets must be expressions

(function (){} )();

[function (){} ()] //[] is equivalent to ()

 

//The following symbols must also be followed by an expression

~function (){} ();

!function (){} ();

+function (){} ();

-function (){} ();

 

delete function (){} ();

typeof function (){} ();

void function(){} ();

 

var f = function () {} ();

 

1,function (){} (); //, must be followed by an expression

1 ^ function (){} ();

1 > function (){} ();

 

4. Type

typeof false //"boolean"
typeof .2//"number"
typeof NaN //"number"
typeof '';//"string"
typeof undefined //"undefined"
typeof Symbol();//"symbol"
 
typeof new Date();//"object"
typeof [] //"object"
typeof alert //"function"
typeof null //"object"

5. Realize the conversion of floating point numbers to integers, or take out the integer part of the numbers

function converInt(num){
        return num >> 0;
}

convertToInt(-Math.PI); // -3

convertToInt(12.921); // 12

A signed right shift converts the left operand to a 32-bit integer

num | 0it is also fine

 

6. A question of this

 

function djw(){
      console.log(this);
}
djw(); // Window
djw.call(null/undefined); // Special case for Number because '/'
djw.call(null);//window
djw.call(undefined);//window
djw.call(1); // Number


function djw(){
      'use strict';
      console.log(this);
}
djw(); // undefined
djw.call(null/undefined); // NaN
djw.call(null);//null
djw.call(undefined);//undefined
djw.call(1); // 1

 In non-strict mode, this defaults to the global object window, and when call or apply explicitly specify the this parameter, the parameter will also be cast to an object (if it is not an object). Among them, null/undefined are replaced with global objects, and underlying types are converted into wrapper objects.

In strict mode, this defaults to undefined, and there is no coercion when call or apply explicitly specify the this parameter. 

 

7. Adding attributes to the base type is invalid

var num=0;
num.prop=1;
console.log(num.prop);

 num.prop is equivalent to Object(num).prop, which means that we add properties to a new object, which has nothing to do with num.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326701028&siteId=291194637