Skip to content

Instantly share code, notes, and snippets.

View LeeeeeeM's full-sized avatar
🎯
专心研究webpack的细节。

EMpersonal LeeeeeeM

🎯
专心研究webpack的细节。
View GitHub Profile
@LeeeeeeM
LeeeeeeM / thousand.js
Created April 14, 2018 05:10
js实现千分符
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"
@LeeeeeeM
LeeeeeeM / animation.js
Created March 23, 2018 06:37
简单动画库,适用于一个动画的循环,不太适用于模拟物理
/**
* 适用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++) {
@LeeeeeeM
LeeeeeeM / iterator_promise.js
Last active March 21, 2018 04:01
async/await内部原理解析。生成器yield原理解析。
// 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);
}
@LeeeeeeM
LeeeeeeM / nextTick.js
Created March 16, 2018 05:38
实现一个nextTick函数
var nextTask = [];
var nextHandler;
function nextTick(fn, cxt = this, args = []) {
nextTask.push(function() {
return fn.apply(cxt, args);
});
if (nextHandler) {
return;
@LeeeeeeM
LeeeeeeM / promise.js
Last active August 31, 2021 01:21
实现一个Promise
// 实现一个promise 拥有then方法
function noop() {
}
function resolve(promise) {
return function(value) {
if (promise._status !== 'pending') {
return; // 如果是fullfilled或者rejected,不可逆
}
@LeeeeeeM
LeeeeeeM / iterator.js
Last active March 15, 2018 09:07
实现迭代器
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;
@LeeeeeeM
LeeeeeeM / inherits.js
Created March 15, 2018 08:21
实现一个inherits方法,满足以下条件,完美继承
// 实现以下功能
// function Animal(name, age) {
// this.name = name;
// this.age = age;
// }
// Animal.prototype.talk = function() {
// return this.name + this.age;
// }
@LeeeeeeM
LeeeeeeM / Observer.js
Created March 15, 2018 02:45
观察者模式
// 可观察对象
function Observable(generator) {
if (!(this instanceof Observable)) {
throw Error('Observable must use new to construct');
}
this._generator = generator;
}
Observable.prototype.subscribe = function(observer) {
@LeeeeeeM
LeeeeeeM / yield_await.js
Last active April 18, 2018 09:39
你真的懂JS的Event-Loop吗???就browser端而言
async function a() {
console.log('step into a');
await b();
console.log('will leave a');
}
async function b() {
await c();
@LeeeeeeM
LeeeeeeM / if.js
Last active March 24, 2018 03:06
if (xxx) 判断
// ** if 里面判断的是布尔值 在js里,只有'', null, undefined, 0, NaN五个值是false
if ([]) {
console.log(1)
}
// console.log(1)
if ({}) {
console.log(1);
}