1,undefined,nan,null,infinity
1) Undefined is the undefined type
var A; Do not assign a value after declaring a variable
TypeOf Type Judging method
Console.log (typeof (a))//undefined
2) NaN is number type is not a digit
var a=123;
var b= "abc";
A-B gets Nan
Console.log (typeof (b)) number
3) Null Object-oriented concept indicates that the object is empty
4) Infinity is the number type, which means that the infinity divisor is 0.
2,js accuracy problem
JS in the operation of the existence of precision problems:
1) can be multiplied by 100 1000 .... First integer in 100 1000
2) number.tofixed (parameter) set reserved decimal place 1.528.toFixed (2) =1.53
Several functions commonly used in 3,math objects
1) Ceiling function Ceil Math.ceil (1.23) =2 up returns the smallest integer
2) Flooring function floor Math.floor (1.23) =1 returns the smallest integer down
3) Random number
Math.random () returns a random number of 0-1
Math.floor (Math.random () *10) returns a random number of 0-9
4) Math.max () Math.min () returns the maximum minimum value
5) Math.Abs (x) returns an absolute value
6) Math.Round (x) rounding
4, data type conversion
1) Type conversions that occur during an implicit conversion variable during operation
!! Console.log (!! " ABC ")
2) display (force) conversion:
Go to String: A, (string) variable b, variable. toString ()
Turn number type: A,number (variable) b,parseint (variable) c,parsefloat (variable)
Boolean type: Boolean (variable)
Several undefined NaN Null 0 false "" for conversion to false
5, short-circuit operation of the logical operator: when the operand is not a bool value
1) Implicit conversion
2) from left to right
3) which operand can determine the result, returns the original operand
"&&0;//true&&false"
6, array detection mode
var arr=[]
1) Array.isarray (arr) has compatibility issues
2) Arr instanceof Array recommended use
3) Object.prototype.toString.Call (arr) can detect any type
7, arguments and ES6 rest usage of functions
1) Arguments only works inside the function and always points to all the arguments passed in by the caller of the current function like an array
2) Rest parameters can only be written in the last, preceded by ... Identity
function foo (a, B, ... rest) {
Console.log (' a = ' + a);
Console.log (' B = ' + B);
Console.log (rest);
}
Foo (1, 2, 3, 4, 5);
Results:
A = 1
b = 2
Array [3, 4, 5]
8, function variable promotion:
First, scan the entire function body statement, and all the declared variables "ascend" to the top of the function
' Use strict ';
function foo () {
var x = ' Hello ', ' + y;
alert (x);
var y = ' Bob ';
}
Foo ();
Although it is strict mode, the statement var x = ' Hello ', ' + y ', not an error, because the variable y is stated later.
But alert shows hello, undefined, indicating that the value of the variable y is undefined.
This is precisely because the JavaScript engine automatically promotes the declaration of the variable y, but does not raise the assignment of the variable Y.
............................................................................................
Code after the variable is lifted:
function foo () {
var y; Declaration of ascending Variable Y
var x = ' Hello ', ' + y;
alert (x);
y = ' Bob ';
}
Note: The weird declaration pattern for variables inside a function
function Fun () {
num=10//Not write Var is equivalent to global variables
}
Fun ()
Console.log (num)//10
9,this pointing problem
1 ' use strict '; 2 var obj={3 Name: "Test", 4 printname:function () {5 Console.log (this.name) 6 } 7 } 8 Obj.printname (); Show Test this refers to obj 9 var obj={11 name: "Test", printname:function () { function ShowName () { console.log (this.name)- return showname () }18 }19 Obj.printname (); This points to undefined (point to Windows in non-strict mode) Workaround: Var obj={22 name: "Test", printname:function () {24 var self=this//Capture THIS25 function ShowName () { console.log (self.name) ShowName () }30 }
10,sort sort of pits
1) the sort () method of the array defaults to converting all elements first to string reordering, and if you sort the numbers directly, you step on the pit.
2) Sort by default according to ASCII code
3) sort is a higher order function, sort (function () {
Write the specific implementation logic
})
Ascending
Sort (function (A, b) {
Return A-B
})
Descending
Sort (function (A, b) {
Return b-a
})
11, Get Style
getComputedStyle (el,null). Width IE does not support
document.getElementById ("Btn"). Currentstyle.width IE provides
function GetStyle (tag, attr) {return tag.currentstyle? Tag.currentstyle[attr]: getComputedStyle (tag,null) [attr]; }
12, the problem of stitching strings
There are performance issues below IE7, which can be replaced with arrays
The problem does not exist above IE7
13, array of several methods
1) Arr.slice (start,end) copies a piece of data from the array, returning the copied array
2) Splice (start,length) returns some elements of the captured array, modifies the original array
More than 2 parameters of splice will add the remaining parameters to the intercepted array position
Arr.splice ("A", "B")
14, empty the array:
1) arr.length=0
2) arr=[]//Recommended Use
3) Arr.splice (0,arr.length)
15. How to avoid the event being overwritten (IE9 not supported below)
Label. AddEventListener (Enventtype,fn,flase) False default bubbling true capture
function Fun () {
Alert ("Hello")
}
Eg:btn.addEventListener ("click", Fun)
Remove event Listener (parameters must be consistent)
Btn.removeeventlistener ("click", Fun)
IE-6-10 (Enventtype Plus on)
tags. attachevent (ENVENTTYPE,FN)
tags. detachevent (ENVENTTYPE,FN)
16, event bubbling, and event capture
Event bubbling: Executing from inside Out, encountering the same event and executing
Event capture: Execution sequence is the opposite of bubbling (deprecated, because IE uses attachevent without a third parameter)
Block event bubbling
E.stoppropagation ()
Block event propagation cancelbubble=true in IE
17, the object function of the event: to record some information when the current event is triggered
Btn.onclick=function (event) {}
Event.target elements that really trigger events
Event.type= "click"
Event.clinetx/clinety
IE Low version incompatible
var tar=e.target| | E.srcelement
2 Methods of 18,json string
1) object-->string json.stringify ()
2) string--> obj json.parse ()
JS Getting Started