Skip to content

Instantly share code, notes, and snippets.

View lushijie's full-sized avatar

Shijie Lu lushijie

  • Meituan && Qihoo 360
  • Beijing
View GitHub Profile
@lushijie
lushijie / shortFunctionCall.js
Created June 2, 2016 01:35
简短版函数调用
function x() {
console.log('x')
};
function y() {
console.log('y')
};
var z = 3;
(z==3?x:y)();
@lushijie
lushijie / chainable.js
Created June 12, 2016 08:47
链式调用
function chainable(fn) {
console.info('a', this);//window
return function () {
console.info('b1', this);
fn.apply(this, arguments);
console.info('b2', this);
return this;
};
}
@lushijie
lushijie / class-weakmap.js
Last active February 12, 2024 00:29
es6 weakMap private property
const privateProps = new WeakMap();
function _convertToLowercase(val) {
return val.toLowerCase();
}
class Person {
constructor(name) {
privateProps.set(this, {name: name});
}
getName() {
return _convertToLowercase(privateProps.get(this).name);
@lushijie
lushijie / AOP.js
Created May 19, 2017 03:33
AOP 切面编程
Function.prototype.before = function(beforefn){
var _self = this; //保存原函数的引用
return function() { //返回包含了原函数和新函数的 ‘代理’函数
beforefn.apply(this, arguments); //执行新函数,修正this,新函数在原函数之前执行
return _self.apply(this, arguments); //执行原函数,并保证this不会被劫持
}
}
Function.prototype.after = function(afterfn) {
var _self = this;
@lushijie
lushijie / runAtOnce.js
Created December 29, 2017 08:37
第一次声明执行并绑定this
let articleRequest = () => {
let f = function() {
// todo
}.bind(this)();
return f;
};
articleRequest();
let articleRequest2 = function f() {
// todo
Function.prototype.bind2 = function(context) {
const self = this;
const args = Array.prototype.slice.call(arguments, 1);
return function() {
const args2 = Array.prototype.slice.call(arguments);
self.apply(context, args.concat(args2));
}
}
@lushijie
lushijie / enclosure.js
Last active February 12, 2024 00:02
通用javascript模块化封装
;(function() {
function MyModule() {
// ...
}
var moduleName = MyModule;
if (typeof module !== 'undefined' && typeof exports === 'object' && define.cmd) {
module.exports = moduleName;
} else if (typeof define === 'function' && define.amd === 'object' && define.amd) {
define(function() {
function interceptNetworkRequests(ee) {
const open = XMLHttpRequest.prototype.open;
const send = XMLHttpRequest.prototype.send;
const isRegularXHR = open.toString().indexOf('native code') !== -1;
// don't hijack if already hijacked - this will mess up with frameworks like Angular with zones
// we work if we load first there which we can.
if (isRegularXHR) {
@lushijie
lushijie / node-or-browser.js
Created December 6, 2018 11:21 — forked from rhysburnie/node-or-browser.js
Detect node or browser
// determine if in-browser or using node.js
// thruthy
var _nodejs = (
typeof process !== 'undefined' && process.versions && process.versions.node);
if (_nodejs) {
_nodejs = {
version: process.versions.node
};
}
@lushijie
lushijie / cubeDebug.js
Last active November 1, 2018 03:43
storage 调试
function datetime(date = new Date(), format) {
if (date && typeof date === 'string') {
const dateString = date;
date = new Date(Date.parse(date));
if (isNaN(date.getTime()) && !format) {
format = dateString;
date = new Date();
}
}
format = format || 'YYYY-MM-DD HH:mm:ss';