Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Happy coding

衍良 chemdemo

👨‍💻
Happy coding
View GitHub Profile
@chemdemo
chemdemo / series-promise.js
Created Jan 13, 2016
Promises series queue
View series-promise.js
'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 Nov 6, 2014
get chinese browsers ua
View cn_ua.js
/*
* @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 Aug 29, 2015
Cross platform way of insertHTML().
View insert_html.js
/**
* @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 Aug 29, 2015
Hexadecimal conversion in js.
View hex_conversion.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 Jun 30, 2014
what happens when you type in a URL in browser
View request_flow.md
  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?)
View min_promise.js
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 Aug 29, 2015
Interview questions
View interview_questions.md
  • 求值:
var n1 = 10;
var n2 = 5;
var r = n1+++n2;
console.log(n1, n2, r); // => 11, 5, 15
  • 求值:
View xss.js
//比如在下列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;
View post_message.js
// 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 Apr 17, 2014
nextTick for browsers and Node.js
View next_tick.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;