Wonderful JavaScript code snippets and javascript snippets

Source: Internet
Author: User

Wonderful JavaScript code snippets and javascript snippets

Wonderful JavaScript code snippets to share with you

1. Obtain the new Array Based on the given conditions on the original array.

Var a = [-1,-1, 1, 2,-2,-2,-3,-3, 3,-3]; function f (s, e) {var ret = []; for (var I in s) {// loop ret based on the original array length. push (e (s [I]);} return ret;} f (a, function (n) {return n> 0? N: 0}); // transmits an anonymous function for logical judgment.

2. More detailed type monitoring methods than native type or typeof

Function type (p) {/function. (\ w *) \ (\)/. test (p. constructor); // obtain the corresponding type through its constructor. Return RegExp. $1 ;}

3. A deep copy of an object or array is used to solve the problem of changing the reference value of an object.

Var copyObject = function (obj) {var result = {}; for (var x in obj) {result [x] = typeof obj = "object "? CopyObject (obj [x]): obj [x] // If the copied value is still an object, the current method is repeated. } Return result ;}

4. Obtain the Cookie value through a regular expression

Function getCookie (name) {if (name & RegExp ("(^ |)" + name + "= ([^;] *) (; | $) "cmd.exe c (document. cookie) return RegExp. $2; // (^ |) does not match the first space. // ([^;] *) Only matches all characters except the; sign. // (; | $) Matches the character ending with ";" or "$ .}

5. Replace "parseInt" with shift operations"

~~3.14 = > 3;

//~~ Round .~ Returns the reverse code of the current value ,~~ Returns the current result (indicating that the "bit" Operation in JS automatically converts the value to an integer)

6. Convert the value to a hexadecimal string (commonly used to represent colors)

(~~ (Math.random() * (1 << 24))).toString(16)

//~~ Bitwise operations are used to obtain the integer.
// <Left shift. Shifts the binary number of 1 to the left by 24 bits. 1 <24 = 2 ^ 24 (the maximum number of colors that can be expressed in RGB mode)
// ToString (16) converts a value to a hexadecimal string.

7. Check the compatibility of object Methods

if ('querySelector' in document) {}

8. NodeList | HTMLCollection | Object conversion to Array or method with Array

NodeList: A list of DOM nodes obtained through the collection method, such as: document. getElementsByTagNmae, document. forms... .
HTMLCollection: HTML block, which is similar to NodeList, but NodeList only supports digital indexing, while HTMLCollection supports name as index.
Both NodeList and HTMLCollection have the following features: they have an array appearance, but they do not have an array method, have a length attribute, and support indexing to read content.

function makeArray(obj) {  var rs = [],    len = obj.length;  try {    rs = [].slice.call(obj, 0);  } catch (e) { //for IE    for (var i = 0; j = obj[i++];) {      rs.push(j);    }  }  return rs;}

9. Clear spaces on both sides of the regular expression match

var trim = function(v){  var patrn = /^\s*(.*?)\s+$/;  return (patrn.test(v))? RegExp.$1 : '  null ';}

10. Time formatting

Function dateFormat (t) {// t is the unit of seconds. Var h = ~~ (T/3600), // t divided by 3600, and the result is an hour. M = ~~ (T % 3600/60), // t calculates the remaining 3600, modulo, and the result is the number of seconds (minutes + seconds) remaining in the hour, divided by 60, and rounded up, the result is minute. S = ~~ (T % 3600% 60); // t calculates the remaining 3600, and then the remaining 60. The remaining is "seconds ". Return h + 'hour '+ m + 'minute' + s + 'second ';}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.