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 / 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)
@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 / 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 / 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 / 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 / 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 / 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
  • 求值:
//比如在下列A、B、C、D三处转义方式均有细节差异。
//<a href="http://www.qq.com" title="<?=$A?>"><?=$B?></a>
//<script>var name="<?=$C?>"</script>
//<textarea>
//<?=$D?>
//</textarea>
//进行pre的只能是D,而且还要做其他转义,比如 < 转为 &lt;
@chemdemo
chemdemo / post_message.js
Created April 17, 2014 12:07
Test postMessage
// see => http://jsperf.com/postmessage
var echoSetTimeout = (function() {
return function(msg, cb) {
setTimeout(cb, 0);
}
})();
var echoWorker = (function() {
var code = 'onmessage = function(e) { postMessage(e.data) };';
var blobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
@chemdemo
chemdemo / next_tick.js
Created April 17, 2014 03:33
nextTick for browsers and Node.js
// see => https://github.com/kriskowal/q/blob/v1/q.js#L83
// Use the fastest possible means to execute a task in a future turn
// of the event loop.
var nextTick =(function () {
// linked list of tasks (single, with head node)
var head = {task: void 0, next: null};
var tail = head;
var flushing = false;
var requestTick = void 0;
var isNodeJS = false;