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 x() { | |
console.log('x') | |
}; | |
function y() { | |
console.log('y') | |
}; | |
var z = 3; | |
(z==3?x:y)(); |
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 chainable(fn) { | |
console.info('a', this);//window | |
return function () { | |
console.info('b1', this); | |
fn.apply(this, arguments); | |
console.info('b2', this); | |
return this; | |
}; | |
} |
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
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); |
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.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; |
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
let articleRequest = () => { | |
let f = function() { | |
// todo | |
}.bind(this)(); | |
return f; | |
}; | |
articleRequest(); | |
let articleRequest2 = function f() { | |
// todo |
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.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)); | |
} | |
} |
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() { | |
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() { |
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 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) { |
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
// 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 | |
}; | |
} |
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 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'; |
NewerOlder