Skip to content

Instantly share code, notes, and snippets.

@dontcry2013
Last active August 12, 2018 11:23
Show Gist options
  • Save dontcry2013/cd92a480f1ef32896539a3b06fc6c44a to your computer and use it in GitHub Desktop.
Save dontcry2013/cd92a480f1ef32896539a3b06fc6c44a to your computer and use it in GitHub Desktop.
js technic
var service = function(){
console.log('task start...');
}
var test = (function(){
var time_start;
return {
before: function(){
time_start = (+new Date());
console.log('cunting...');
},
after: function(){
var end = (+new Date()) - time_start;
console.log('counting stop:' + end);
}
}
})();
var aop = function(fn, proxy){
proxy.before && proxy.before();
fn();
proxy.after && proxy.after();
}
aop(service, test);
var op = {a: 1};
var a = 2;
(function(){
console.log(this.a);
})() // 2
(function(){
console.log(this.a);
}()) // 2
(function(){
console.log(this.a)
}.apply(op))
(function(){
console.log(this.a)
}).call(op)
(function(){
console.log(this.a);
}).bind(op)() // 1
(function(){
console.log(this.a);
}.bind(op)()) // 1
(function(arg1, arg2){
console.log(this, arg1, arg2)
}).call(op, "ss", "sss")
(function(arg1, arg2){
console.log(this, arg1, arg2)
}).apply(op, ["ss", "sss"])
setTimeout(function(arg){
console.log(op, arg)
}.call(op, "ss"), 19, "sss") // {a: 1} "ss"
var chunk = function(arr, process, count){
setTimeout(function(){
for(var i = 0; i < Math.min(count, arr.length); i++) {
process(arr.shift());
}
if(arr.length > 0) {
setTimeout(arguments.callee, 100);
}
}, 100);
}
chunk([1,2,3,4,5,6,7,8,9,10], function(x){console.log(x);}, 1)
var auto_increase = function(){
var id=0;
return function(){
return ++id;
}
}();
var increaser = (function(){
var i = 0;
var obj = {};
obj.reset = function(){
i = 0;
console.log("reset", i);
}
obj.increase = function(){
++i;
console.log("increase", i);
}
return obj;
}())
// 1
auto_increase();
// 2
auto_increase();
// newbie
var gotop = function(){
if(/firefox/i.test(navigator.userAgent)) {
document.documentElement.scrollTop = 0;
} else {
document.body.scrollTop = 0;
}
}
// veteran
var gotop = (function(){
var isFF = /firefox/i.test(navigator.userAgent);
var docEl = document[ isFF ? 'documentElement' : 'body' ];
return function(){
docEl.scrollTop = 0;
}
})();
var getData = (function(){
var onAjax = false;
return function(callback){
if(!onAjax) {
onAjax = true;
$.get('/xxx', function(data){
callback(data);
onAjax = false;
});
}
}
})();
$(window).scroll(function(){
if(bottom) {
getData(render);
}
});
// debounce
function _debounce(fn,wait){
var timer = null;
return function(){
clearTimeout(timer)
timer = setTimeout(()=>{
fn()
},wait)
}
}
function _log(){
console.log(1)
}
window.onscroll = _debounce(_log,500)
//throttle
function _throttle(fn,wait,time){
var previous = null; //record previous time
var timer = null;
return function(){
var now = +new Date();
if(!previous) previous = now;
//if the gap longer than the throttle, run actively
if(now - previous > time){
clearTimeout(timer);
fn();
previous = now;
}else{
clearTimeout(timer);
timer = setTimeout(function(){
fn();
},wait);
}
}
}
function _log(){
console.log(1)
}
window.onscroll = _debounce(_log,500,2000)
// Service container
var services = {
A: () => {console.log(1)},
B: () => {console.log(2)},
C: () => {console.log(3)}
}
// Service class, register dependency
function Service(A, B) {
A()
B()
}
getFuncParams = function (func) {
var matches = func.toString().match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m);
if (matches && matches.length > 1)
return matches[1].replace(/\s+/, '').split(',');
return [];
}
setFuncParams = function (params) {
for (var i in params) {
params[i] = services[params[i]];
}
return params;
};
// Injector
function Activitor(func, scope) {
return () => {
func.apply(scope || {}, setFuncParams(getFuncParams(func)));
}
}
// Instantiation
var service = Activitor(Service);
service();//1 2
var MODULE_TWO = (function (old) {
var my = {},
key;
for (key in old) {
if (old.hasOwnProperty(key)) {
my[key] = old[key];
}
}
var super_moduleMethod = old.moduleMethod;
my.moduleMethod = function () {
// override method on the clone, access to super through super_moduleMethod
};
return my;
}(MODULE||{}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment