A collection of tricks in Javascript-js

Source: Internet
Author: User

1, for String,number and other basic types, = = and = = = There is a difference1) Comparison of different types, = = Comparison of "converted to the same type of value" to see if "value" is equal, = = = If the type is different, the result is not equal to 2) the same type comparison, direct "value" comparison, the results of the same 2, for Array,object and other advanced types, = = and = = = There is no difference betweenMake a "pointer address" comparison 3, the basic type and the advanced type, = = and = = = There is a difference1) for = =, the advanced conversion to the underlying type, the "value" Comparison 2) because the type is different, = = = The result is False


Global Object

Property: Infinity/nan/undefined/null

Method: eval ()/isfinite ()/IsNaN ()/parsefloat ()/parseint ()/decodeURI ()/decodeURIComponent ()

encodeURI ()/encodeURIComponent ()


Single built-in object eval and math

Eval () parses the string into a JS script and executes

1 string  str = ‘console.log("this is eval")‘;

Under strict scope, eval () is a field that does not affect the value outside

It is not recommended to use eval, low efficiency, easy to be cross-domain attack, can not use eval as far as possible without

Usage of strict scopes

123456 ‘use strict‘varnumtemp = 1;console.log("numtemp:",numtemp);eval(‘var numtemp = 2;console.log("numtemp:",numtemp)‘);console.log("numtemp:",numtemp);//eval里面的内容不会影响外面的变量

Properties of the Math object

Math.PI

Math.e

Math.sqrt2

Math.max ()

Math.min ()

Math.Round () rounding

Math.ceil () a decimal nearest one

Math.floor () Rounding out decimal place

Math.random () generates a random number within 0-1

Random number in the generated range

123 functionrandBetween(min,max){             returnmin + Math.random*(max-min);}


Two ways to Access object members

Dot notation

Bracket notation

Object.keys (obj) The use of key-valued functions

123 varobj = {a:1,b:2,c:3};Object.keys(obj);  // => [‘a‘,‘b‘,‘c‘]Object.keys(obj).length // => 3

ToString function

12 vararr = Object.keys(obj);arr.toString();  // => ‘a,b,c‘

Join function

12 vararr = Object.keys(obj);arr.join("ss");  // => ‘assbssc‘



Array

Initialize, check

1. Everything can be put in the array, JS 7 objects can be put

2. The array is indexed starting at 0

Initialize method

1. Constructor methods

123 var arr = new array ();  //empty array var arr = new array ( 3 );  //declaration length 3 Array var arr = new array ( ' Jing ' , ' ya ' );  //declares an array of length 2

2. Literal method (cannot directly specify the number of members)

1234 var arr = []; var arr = [ ' Jingya ' , Code class= "AS3 string" > ' ya ' ]; arr[ 3 ] = ' Xiaolizi ' //automatically create this member arr[ 6 ] = Code class= "AS3 string" > ' new ' ;       //automatically create 4, 5 members undefined

Array checking (arrays are not part of the base data type, object is also, so typeof cannot be used)

Cannot use TypeOf

instanceof Keywords

1 arr instanceofArray// 返回true

Array.isarray ()

1 Array.isArray(arr);  // 返回的还是true


Converting and sorting


In the order of the strings, the array itself will be modified

Sort (), this sort can also put functions

123456 vararr = [11,2,3,23,55];function compareAB(a,b){     returna>b;}arr.sort(compareAB);console.log(‘after sort ‘,arr);


Reverse ()

Stack and queue operations

Stack operation, LIFO

Push () Backward, returns the length of a new array with parameters

Pop () out, returns the number that pops up without parameters

Queue operation, advanced back out

Push () Press the data back in

Shift () Previous popup data

Unshift () from previous press-in data

Other operations

Arr.concat (ARR1) connection between two arrays

Arr.slice (2,5)//Only take 2,3,4, not 5, does not affect the original array itself

Arr.splice (first)//starting from index 1 to intercept a return array, the original array has changed

Arr.splice (1,0,false, ' str ')//delete 0 from 1th bit, insert false and str two characters

IndexOf ()/lastIndexOf ()//

IndexOf (5)//Find the words to return to the index position, if not found return-1

Every ()/some ()/ForEach ()/filter ()/Map ()

is to pass in a function as a parameter and execute the function sequentially on each parameter passed in.

Every () if the execution of each function returns True, then the result of every will return true

Some () as long as a function execution returns true, some returns true

Filter () Returns a new array of all the members whose execution returns true

Map () processes each member with a function and composes the result into a new array to return

ForEach () has no return value, simple execution function


Reduce ()


Reduceright ()







From for notes (Wiz)

A collection of tricks in Javascript-js

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.