Common questions distal end surface portion JS

1. Closure

2.JS operations and access to cookie

//创建cookie
function setCookie(name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
    if (expires instanceof Date) {
        cookieText += '; expires=' + expires;
    }
    if (path) {
        cookieText += '; expires=' + expires;
    }
    if (domain) {
        cookieText += '; domain=' + domain;
    }
    if (secure) {
        cookieText += '; secure';
    }
    document.cookie = cookieText;
}

//获取cookie
function getCookie(name) {
    var cookieName = encodeURIComponent(name) + '=';
    var cookieStart = document.cookie.indexOf(cookieName);
    var cookieValue = null;
    if (cookieStart > -1) {
        var cookieEnd = document.cookie.indexOf(';', cookieStart);
        if (cookieEnd == -1) {
            cookieEnd = document.cookie.length;
        }

 3.js array deduplication

of arr1 function () { 
  var n-= []; // a new temporary array 
  for (var i = 0; i <this.length; i ++) // iterate current array 
  { 
    // if the current i-th array has been saved into the the temporary array, skip, 
    // otherwise the current temporary array to push items inside 
    IF (n.indexOf (the this [I]) == -1) n.push (the this [I]); 
  } 
  return n-; 
} 

arr2 is function () { 
    var n-= {}, r = []; // n-hash table is, r is the temporary array 
    for (var i = 0; i <this.length; i ++) // iterate current array 
    { 
        IF ( ! n [this [i]] ) // If this is not the hash table entry 
        { 
            n-[the this [I]] = to true; // stored hash table 
            r.push (this [i]); // the current array push the current item to a temporary array which 
        } 
    } 
    return R & lt; 
} 
 
function ARR3 () {
    var n-= [the this [0]]; // array results 
    for (var i = 1; i <this.length; i ++) // from the second traversing 
    { 
        // If the i-th item of the array in the current array location is not the first occurrence of i, 
        // so i represents the item is repeated, ignored. Otherwise, the resulting array into 
        IF (this.indexOf (the this [I]) == I) n.push (the this [I]); 
    } 
    return n-; 
}

 4.

 

Guess you like

Origin www.cnblogs.com/minty/p/11288109.html