JavaScript programming learning skills summary

  • 2020-12-26 05:34:38
  • OfStack

This article shares JavaScript programming learning skills for your reference, the specific content is as follows

1. Variable conversion


varmyVar="3.14159",

str=""+myVar,//tostring

int=~~myVar,//tointeger

float=1*myVar,//tofloat

bool=!!myVar,/*toboolean-anystringwithlength

andanynumberexcept0aretrue*/

array=[myVar];//toarray

But the conversion date (new Date(myVar)) and the regular expression (new RegExp(myVar)) must use constructors, and the regular expression is created using simplified forms such as /pattern/flags.

2. Integer and convert to numeric type


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2

3. Date rotation value


//JS The internal representation of time itself is Unix Timestamp, which records the current distance in milliseconds 1970 years 1 month 1 day 0 A unit of time for a point 

vard=+newDate();//1295698416792

4, class array object to array


vararr=[].slice.call(arguments)

 The following example is even better 

functiontest(){

varres=['item1','item2']

res=res.concat(Array.prototype.slice.call(arguments))// methods 1

Array.prototype.push.apply(res,arguments)// methods 2

}

5. Conversion between bases


(int).toString(16);//convertsinttohex,eg12=>"C"

(int).toString(8);//convertsinttooctal,eg.12=>"14"

parseInt(string,16)//convertshextoint,eg."FF"=>255

parseInt(string,8)//convertsoctaltoint,eg."20"=>16

 will 1 An array is inserted into the other 1 The position specified by an array 

vara=[1,2,3,7,8,9];

varb=[4,5,6];

varinsertIndex=3;

a.splice.apply(a,Array.prototype.concat(insertIndex,0,b));

6. Delete array elements


vara=[1,2,3,4,5];

a.splice(3,1);//a=[1,2,3,5]

You might wonder why you want to use splice instead of delete, because using delete will leave a hole in the array, and the index doesn't decrement.
7. Judge whether it is IE


varie=/*@cc_on!@*/false;

ie is too... ie is too...

There are many more ways to do this. See below


//edithttp://www.lai18.com

// Looks like the shortest. Use IE Does not support standards ECMAscript The mechanism by which the commas at the end of an array are ignored 

varie=!-[1,];

// By using the IE Conditional annotation of 

varie=/*@cc_on!@*/false;

// Conditional comment again 

varie//@cc_on=1;

//IE Vertical tabs are not supported 

varie='v'=='v';

// The principle of same 
varie=!+"v1";

Learn this moment feel too weak.

Use native methods whenever possible

To find the largest number in a set of 1, we might write a loop, such as:


varnumbers=[3,342,23,22,124];

varmax=0;

for(vari=0;i

if(numbers[i]>max){

max=numbers[i];

}

}

alert(max);

In fact, using the native method, can be more simple to implement


varnumbers=[3,342,23,22,124];

numbers.sort(function(a,b){returnb-a});

alert(numbers[0]);

Of course, the simplest way to do this is:


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
0

The same can be said for the present


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
1

8. Generate random numbers


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
2

You don't swap the values of the two variables with the third variable


a=[b,b=a][0];

9. Event delegation

The js code is as follows:


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
4

Use event delegation to write something more elegant:


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
5

10. Test ie version


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
6

javaScript version detection

Do you know which version of Javascript your browser supports?


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
7

11. Determine whether the attribute exists


//BAD:Thiswillcauseanerrorincodewhenfooisundefined

if(foo){

doSomething();

}
//GOOD:Thisdoesn'tcauseanyerrors.However,evenwhen

//fooissettoNULLorfalse,theconditionvalidatesastrue

if(typeoffoo!="undefined"){

doSomething();

}
//BETTER:Thisdoesn'tcauseanyerrorsandinaddition

//valuesNULLorfalsewon'tvalidateastrue

if(window.foo){

doSomething();

}

In some cases, we have deeper structures and need more appropriate inspections


// When a character variable is involved in the operation, JS Automatically converts it to a numeric type (if not, to NaN ) 

'10.567890'|0

// The results of :10

//JS All numeric types inside are double - precision floating - point numbers , As a result, JS When a bit operation is performed, these numeric operands are first converted to integers, and then the operation is performed 

//| is 2 Hexadecimal or, x|0 Always equal to the x ; ^ Is different or identical 0 different 1 , so x^0 Or is it always equal to x ; As for the ~ It's going to be the reverse bit, but of course it's going to be the reverse bit 1 The sample of 

'10.567890'^0

// The results of :10

-2.23456789|0

// The results of :-2
9

In fact, the best method to detect the existence of an attribute is:


if("opera"inwindow){

console.log("OPERA");

}else{

console.log("NOTOPERA");

}

12. Check whether the object is an array


varobj=[];

Object.prototype.toString.call(obj)=="[objectArray]";

That is the end of this article, I hope you learn javascript programming is helpful.


Related articles: