This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function thousand(num) { | |
var num = num.toString(); | |
var reg = num.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(\d{3})+\b)/g; | |
return num.replace(reg, function($0, $1) { | |
return $1 + ',' | |
}) | |
} | |
thousand(231231) // "231,231" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 适用css3 transform进行动画,有的库会使用Object.defineProperty | |
*/ | |
var requstAnimationFrame = (function() { | |
var prefix = ['moz', 'ms', 'webkit', 'o']; | |
var raf, caf; | |
if (raf = window.requestAnimationFrame) { | |
caf = window.cancelAnimationFrame; | |
} else { | |
for (var i = 0; i < prefix.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES next中async/await proposal实现原理是什么? - 何幻的回答 - 知乎 | |
// https://www.zhihu.com/question/39571954/answer/82042148 | |
// 实现依赖迭代器的await 配合js引擎的ast 解析* 和 yield | |
// 如果不懂promise实现原理,请看我的其他gist,有个实现promise的 | |
function yieldPromise(generator) { | |
var iterator = generator(); | |
recursiveCore.call(iterator); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nextTask = []; | |
var nextHandler; | |
function nextTick(fn, cxt = this, args = []) { | |
nextTask.push(function() { | |
return fn.apply(cxt, args); | |
}); | |
if (nextHandler) { | |
return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 实现一个promise 拥有then方法 | |
function noop() { | |
} | |
function resolve(promise) { | |
return function(value) { | |
if (promise._status !== 'pending') { | |
return; // 如果是fullfilled或者rejected,不可逆 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = { | |
isType: function(value, type) { | |
return Object.prototype.toString.call(value).toLowerCase().slice(8, -1) === type; | |
} | |
}; | |
function Iterator(list) { | |
list = util.isType(list, 'array') ? list : [list]; | |
this._array = list; | |
this._index = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 实现以下功能 | |
// function Animal(name, age) { | |
// this.name = name; | |
// this.age = age; | |
// } | |
// Animal.prototype.talk = function() { | |
// return this.name + this.age; | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 可观察对象 | |
function Observable(generator) { | |
if (!(this instanceof Observable)) { | |
throw Error('Observable must use new to construct'); | |
} | |
this._generator = generator; | |
} | |
Observable.prototype.subscribe = function(observer) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function a() { | |
console.log('step into a'); | |
await b(); | |
console.log('will leave a'); | |
} | |
async function b() { | |
await c(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ** if 里面判断的是布尔值 在js里,只有'', null, undefined, 0, NaN五个值是false | |
if ([]) { | |
console.log(1) | |
} | |
// console.log(1) | |
if ({}) { | |
console.log(1); | |
} |