Giter VIP home page Giter VIP logo

blog-ynchuan's People

Contributors

ynchuan avatar

Watchers

 avatar  avatar

blog-ynchuan's Issues

http header host

  • http请求发出后,首先客户端会将请求url转化为http报文,其中包括请求头和请求体,请求头中host根据当前url可以得到,同时host可以人为修改为与当前url域名不一致的情况
  • 只要服务器能够在同一台机器(即,在同一个 IP 地址)上提供多个不同的主机名,服务器就可以通过 Host 首部,根据主机名来区分不同的相对 URL,所以服务器知道域名的方式就是从header的host中获取,请求的域名是无法直传给服务器的
  • http报文构造完成,报文被发送到tcp层,tcp层会依据原始请求域名通过dns获取目标机器ip,将http报文封装成传输层报文,然后转递到ip成->数据链路层->物理层,完成封装发送

for-of & for-in

  • for-of 遍历循环需要被遍历对象继承iterator属性,例如array,且遍历的是value而非key
  • for-in 遍历的是对象的key,数组对象遍历索引下标
var obj = {
    a: 'obja',
    b: 'objb',
    c: 'objc',
    d: 'objd',
};
var arr = ['arr1', 'arr2', 'arr3', 'arr4'];

for (var key in obj) {
    console.log(key);
    console.log("====" + obj[key]);
}

for (var key in arr) {
    console.log(key);
    console.log("====" + arr[key]);
}

for (var key of arr) {
    console.log(key);
}

for (var key of obj) { //报错,没有继承iterator
    console.log(key);
}

事件代理模拟

利用事件冒泡实现类似$('dom').on('click','.name,.age',function(e){})调用的函数;

flex

容器标签属性

  1. display:flex
  2. flex-direction: row || column || row-reverse || column-reverse; 决定子元素排列方向,决定main-axis
  3. flex-wrap: wrap || nowrap || wrap-reverse; 决定子元素过多时换行情况;纵列时过多从上向下换列
  4. flex-flow是flex-direction和flex-wrap的速记,先写direction后写wrap
  5. justify-content: flex-start || flex-end || center || space-between || space-around 决定main-axis单行方向上的对齐方式
  6. align-items: flex-start || flex-end || center || stretch || baseline 决定cross-axis方向的单列元素对齐方式
  7. align-centent: flex-start || flex-end || center || stretch(沿Cross-Axis填充) 决定多行多列的对齐方式

项目属性

  1. order: 类似z-index,值越大越在后面,默认都是0
  2. flex-grow: 默认0,关闭自动放大
  3. flex-shrink: 默认1,开启自动缩小
  4. flex-basis: 指定项目的初始大小,默认的值是auto。flex-basis可以取任何用于width属性的任何值。比如 % || em || rem || px等
  5. flex: 是flex-grow、flex-shrink和flex-basis三个属性的速记。默认值flex: 0 1 auto; | flex: 1 1 auto | flex: 0 0 auto | flex: 2 1 0(扩张比例大)
  6. align-self: auto || flex-start || flex-end || center || baseline || stretch 改变一个弹性项目沿着侧轴的位置,而不影响相邻的弹性项目

throttle & debounce

/**
 * 函数节流(throttle):某个动作规定的时间内只执行第一次
 */

const throttle = (action, idle) => {
    let last = 0;
    return (...prop) => {
        let curr = +new Date;
        if (curr - last > idle) {
            action.apply(this, prop);
            last = curr;
        }
    }
};

const throttleAction = throttle((...prop) => {
    console.log(prop);
}, 1000);

throttleAction('throttle1');
throttleAction('throttle2');
throttleAction('throttle3'); // 输出 throttle1

setTimeout(() => {
    throttleAction('throttle4');
    throttleAction('throttle5');
    throttleAction('throttle6'); // 输出 throttle4
}, 2000);


/**
 * 函数防抖 (debounce):某个动作规定的时间内只执行最后一次
 */

const debounce = (action, idle) => {
    let handler;
    return (...prop) => {
        handler && clearTimeout(handler);
        handler = setTimeout(() => {
            action.apply(this, prop);
        }, idle);
    }
};

const debounceAction = debounce((...prop) => {
    console.log(prop);
}, 1000);

debounceAction('debounce1');
debounceAction('debounce2');
debounceAction('debounce3'); // 输出 debounce3

setTimeout(() => {
    debounceAction('debounce4');
    debounceAction('debounce5');
    debounceAction('debounce6'); // 输出 debounce6
}, 2000);

map

/**
 * 由于map具有有序性和一一映射的特性,所以map可以转换为数组和对象
 * map->array 二维数组形式,可逆
 * map->object 键值对形式,可逆
 */


let mapToObj = (map) => {
    let obj = Object.create(null);
    for ([key, value] of map.entries()) {
        obj[key] = value;
    }
    return obj;
}

let map = new Map();
map.set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4]);
console.log("map to object test:");
console.log(mapToObj(map));


let objToMap = (obj) => {
    let ret = new Map;
    for (let key in obj) {
        ret.set(key, obj[key]);
    }
    return ret;
};

let obj = { 'a': 1, '2': 'b' };
console.log("object to map test:");
console.log(objToMap(obj));


let arrToMap = (arr) => {
    return new Map(arr);
};
let arr = [
    [1, 'a'],
    [2, 'b'],
    [4, 'd']
];
console.log("array to Map test:");
console.log(arrToMap(arr));


let mapToArr = (map) => {
    return [...map];
};
console.log("map to array test:");
console.log(mapToArr(new Map().set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4])));



let jsonToMap1 = (json) => {
    return objToMap(JSON.parse(json));
};
let jsonToMap2 = (json) => {
    return new Map(JSON.parse(json));
};
let json1 = '{"a":"a","b":"b"}';
console.log("json to map test 1:");
console.log(jsonToMap1(json1));

let json2 = '[["a","1"],["b"],[3,"c"]]';
console.log("json to map test 2:");
console.log(jsonToMap2(json2));




let mapToJson = (map) => {
    return JSON.stringify(mapToObj(map));
};
console.log("map to array json:");
console.log(mapToJson(new Map().set('a', 'a').set('c', 1).set({}, {}).set([1, 2], [3, 4])));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.