Skip to content

Instantly share code, notes, and snippets.

View chemdemo's full-sized avatar
👨‍💻
Happy coding

衍良 chemdemo

👨‍💻
Happy coding
View GitHub Profile
@chemdemo
chemdemo / interview_questions.md
Last active August 29, 2015 14:02
Interview questions
  • 求值:
var n1 = 10;
var n2 = 5;
var r = n1+++n2;
console.log(n1, n2, r); // => 11, 5, 15
  • 求值:
@chemdemo
chemdemo / min_promise.js
Last active August 29, 2015 14:02
mini Promise
var Promise = function () {
this.thens = [];
};
Promise.prototype = {
resolve: function () {
var t = this.thens.shift(), n;
t && (n = t.apply(null, arguments), n instanceof Promise && (n.thens = this.thens));
},
then: function (n) {
@chemdemo
chemdemo / request_flow.md
Created June 30, 2014 04:46
what happens when you type in a URL in browser
  1. browser checks cache; if requested object is in cache and is fresh, skip to #9
  2. browser asks OS for server's IP address
  3. OS makes a DNS lookup and replies the IP address to the browser
  4. browser opens a TCP connection to server (this step is much more complex with HTTPS)
  5. browser sends the HTTP request through TCP connection
  6. browser receives HTTP response and may close the TCP connection, or reuse it for another request
  7. browser checks if the response is a redirect (3xx result status codes), authorization request (401), error (4xx and 5xx), etc.; these are handled differently from normal responses (2xx)
  8. if cacheable, response is stored in cache
  9. browser decodes response (e.g. if it's gzipped)
  10. browser determines what to do with response (e.g. is it a HTML page, is it an image, is it a sound clip?)
@chemdemo
chemdemo / hex_conversion.js
Last active August 29, 2015 14:05
Hexadecimal conversion in js.
// 十进制转其他
var x=100;
alert(x);
alert(x.toString(8));
alert(x.toString(32));
alert(x.toString(16));
//其他转十进制
var x='100';
alert(parseInt(x,2));
alert(parseInt(x,8));
@chemdemo
chemdemo / insert_html.js
Last active August 29, 2015 14:05
Cross platform way of insertHTML().
/**
* @param {HTMLElement} el
* @param {String} where beforeBegin、afterBegin、beforeEnd、afterEnd
* @param {String} html
*/
function insertHTML(el, where, html) {
if (!el) return false;
where = where.toLowerCase();
@chemdemo
chemdemo / cn_ua.js
Created November 6, 2014 15:01
get chinese browsers ua
/*
* @Author: dm.yang
* @Date: 2014-09-19 15:40:08
* @Last Modified by: dm.yang
* @Last Modified time: 2014-11-06 22:36:51
*/
var uaParser = require('ua-parser');
@chemdemo
chemdemo / series-promise.js
Created January 13, 2016 11:42
Promises series queue
'use strict';
let p = (v) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000)
}).then(() => v)
}
[1, 2, 3, 4, 5].reduce((seq, curr) => {
return seq.then(() => p(curr)).then(console.log)