JS--Bear

Source: Internet
Author: User
Tags array to string new set

1. Methods for judging the empty object

1) Object.keys ({}). length = = 0//True

2) object.getownpropertynames ({}). length = = 0//True

3) json.stringify ({}) = = = ' {} '//True

2. Determine whether the array

1) Array.isarray ([])//True

2) Object.prototype.toString.call ({}) = = = ' [Object Array] '; True

3) [].constructor.tostring (). IndexOf (' Array ')!==-1; True

3.js variables

1) A variable declared with Var, whose scope is within the function where the statement is located, and there is a variable elevation phenomenon;
Use a let declaration of a variable whose scope is within the code block where the statement resides, and there is no variable elevation;
A constant is declared with const, and the value of the constant cannot be modified in the code that appears later.

2) JS variable promotion: The variable can be declared after use, that is, the variable can be used before the declaration

4. Four ways to create objects

1) How object literals are created
var obj = {name: ' Tom ', age:23}

2) factory-style object creation
function Createobj () {
var obj = new Object ();
Obj.name = ' Tom ';
}

3) Constructor method to create an object
function Createobj () {
THIS.name = ' Tom ';
}
var obj = new Createobj ();

4) using the prototype chain method
function Createobj () {}
CreateObj.prototype.name = ' Tom ';
var obj = new Createobj ();

Operation of 5.object

1) Object.keys gets all keys values for an object in an array form

2) Object.values gets all values of the object as an array form

3) {}.hasownproperty (' key ') determines if the object has a key property

4) for (x in obj) Loop object

6.number

1) toString turns into a string in parentheses to add cardinality

2) toFixed turns into a string with parentheses specifying the number of digits after the decimal point is retained

3) toLocaleString formatted numbers, every three commas

4) toexponential turns into an exponential representation

7.Math

1) Math.Round (0.6)//1 rounding

2) Math.ceil (0.4)//1 Rounding up

3) Math.floor (0.9)//0 rounding down

4) Math.max (1,2,3,4)//4

5) Math.min (1,2,3,4)//1

6) Math.random () * (Max-min + 1) + min//random number

8.Date

1) date.getfullyear ()

2) Date.getmonth ()

3) Date.getdate ()

4) Date.getday ()//Get the day of the week

5) Date.gethours ()

6) Date.getminutes ()//min.

7) Date.getseconds ()//sec

8) Date.getmillseconds ()//MS

9) Number of milliseconds date.gettime ()//1970.1.1 to date

9.array

1) concat ()//Connect multiple arrays and return results

2) Join ()//Divide the array into strings such as [1,2].join (', ') = "+"

3) Pop ()//delete and return the last element of the array

4) Pusp ()//Add one or more elements to the end of the array and return the new length

5) Unshift ()//Add one or more elements to the beginning of the array and return the new length

6) Shift ()//delete and return the first element of the array

7) sort ()//array sorting

8) reverse ()//inverted array

9) toString ()//convert array to string

Slice (start, [end])//Returns an array of specified elements

Splice (index, count, [Add])//delete the specified element from the array and return the deleted content (array format)
Index: Location; count: Delete quantity; add: Element optional
[1,2,3].splice ( -1,1) = []
[1,2,3].splice (1,2,4) =>[1,4]

10.string

1) Slice (start, [end])//intercept string

2) substring (start, [stop])//intercept string, similar to slice, but does not allow negative arguments, if the argument start is equal to stop , then the method returns an empty string (that is, a string of length 0 )。 If start is larger than stop , the method swaps both parameters before extracting the substring.

3) substr (start, length)//intercept string

4) CharAt ()//returns the character at the specified position

5) Concat ()//stitching string

6) IndexOf ()//Retrieve string

7) LastIndexOf ()//search from backward forward

8) match ()//Find matches with regular (' Match1match2 '). Match (/\d+/g) = [+];

9) Replace ()//replace the value that matches the regular (' Match1match2 '). Replace (/\d/g, ' | | ') = (' match| | match| | ')

Search ()//retrieves the value (' Match1match2 ') that matches the regular match. Search (/\d/g) = 5

One) toUpperCase ()//convert string to uppercase

toLowerCase ()//convert string to lowercase

Split (' & ') string is split by ' & '

One. JS global function

1) Encodeurl () encode the string as a URL

2) Decodeurl () decode a URL

3) Escape () encode the string

4) decoding the Unescape ()

5) parsefloat () parses a string and returns a floating-point number

6) parseint () parses a string and returns an integer

7) Number () to convert the value of the object to a digital

8) string () to convert the value of the object to a string

JS Regular

1)/Regular body/modifier ==>/^\d+$/g or var r = new RegExp (' \\d ', ' G ')
/^1[34578]\d{9}$/g and/\d+.\d+/g

2) modifier
I: ignoring case;
G: Perform global match;
M: Perform multi-line matching

3) square brackets
[ABC]: Find any character between square brackets
[^ABC]: Find any characters that are not in square brackets
[0-9]: Find any number of 0-9
[A-za-z]: Find any characters from small write a to uppercase Z

4) meta-characters
\w Finding Word characters
\d Finding numbers
\d finding non-numeric characters
\s finding whitespace characters
\b Finding word boundaries

5) quantifier
n+ matches any string that contains at least one n
n matches any character that contains 0 or more n
N? Match any character that contains 0 or one n
N{x} matches a character containing x n
N{x,} n matches at least x times consecutive occurrences
N{x,y} n appears consecutively at least X times and matches at most y times
n$ matches any character ending with n
^n matches any character that starts with N

13.http Request

1.get Request
1) var xhr = new XMLRequest ()
2) Xhr.onreadystatechange = function () {
if (xhr.readystate = = = 4 && xhr.status = = = | | xhr.status = = 304)//Successful callback function
}
3) Xhr.onerror = function () {}
4) Xhr.open (' GET ', url, true)//true: Whether it is an asynchronous request
5) Xhr.send ()

2. Post request
1) var xhr = new XMLRequest ()
2) Xhr.onreadystatechange = function () {
if (xhr.readystate ===4 && xhr.status = = = | | xhr.status = = 304) {}
}
3) Xhr.open (' POST ', url, True)
4) Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ')
5) xhr.send (' name=tom&age=12 ')

14. Miscellaneous

The difference between 1.getXX and queryxx:
1). Getxxxbyxxx gets a dynamic collection, Queryselector gets a static collection
2). GetXXX can accept the wrong parameters, queryxx strictly conforms to the CSS selector specification
3). GETXX Performance is fast
Use occasions:
Normal use of getxx, if the resulting elements need to be cumbersome multiple times getelementby. Combination to get the words using Queryselector, convenient

2. Transfer argument to a group

var args = Array.prototype.slice.call (arguments);

3. Digital Formatting

var n = 123456789;

N.tolocalestring () ==> "123,456,789"

4. Array de-weight

[... new Set ([1, "1", 2, 1, 1, 3])] = = [1, ' 1 ', 2, 3]

5. Array padding

Arr.fill (value[, start[, end]])

Array (6). Fill (8) = = [8,8,8,8,8,8]

6. One line of code a plugin

"★★★★★☆☆☆☆☆". Slice (5-rate, 10-rate);

7.+ A unary operation can be a string converted to number

+ New Date = = "1525186216452

+ ' 1.1 ' = = 1.1

8.js Input Monitoring

var input = document.getElementById (' input ');
Input.oninput = function (EV) {}

9. Short-circuit operation var a = b && 1

Equivalent
if (b) {a = 1
} else {
A = b
}var A = B | | 1

Equivalent
if (b) {a = b
} else {a = 1
}

10.CSS aspects

About CSS sub-elements floating stepfather element height does not automatically open up the problem
1. Add a style to the parent element: Overflow:hidden;
2. Add an empty element at the end of the parent element, plus the empty element style: Clear:both;
3. Float the parent element together;

11. Summary: What is the difference between XML and HTML?

1) XML tags can be arbitrarily specified, but the HTML tag is fixed;
2) XML syntax is very strict, HTML syntax is very loose;
3) XML is used to describe and store data-similar to a database; HTML is used to represent the structure of a Web page, only for browsers;

12.dom operation

1. Append at the end of the parent element:
Parent.appendchild (Child)
2. Before inserting to an existing child element under the parent element:
Parent.insertbefore (Child, Oldchild)
3. Replace the existing child element under the parent element
Parent.replacechild (Child, Oldchild)

Copy Objects

var b=JSON.parse(JSON.stringify(a))

Exchange two values

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

15. Add a border to each element of the page

[].foreach.call ($$ ("*"), function (a) {
A.style.outline= "1px solid #" + (~ ~ (Math.random () * (1<<24)). toString (16)
})

JS--Bear

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.