View AOP.js
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;
View arrayReduce.js
// reduce方法
let arr = [1, 2, 3, 4, 5, 6];
let reduceResult = arr.reduce((x, y) => {
return x + y
}, 123);
console.log(reduceResult);
View arrayUnique.js
Array.prototype.unique = function() { //查找索引 indexOf
var ret = []
for (var i = 0; i < this.length; i++) {
if (ret.indexOf(this[i]) == -1) {
ret.push(this[i])
}
}
return ret;
}
Array.prototype.unique = function() { //借用对象
View class-weakmap.js
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);
View compareVerison.js
var versions= [{"version":"1.3.0-beta.2"},{"version":"1.3.0-beta.19"},{"version":"1.3.0-beta.18"},{"version":"1.3.0-beta.17"},{"version":"1.3.0-beta.16"},{"version":"1.3.0-beta.15"},{"version":"1.3.0-beta.14"},{"version":"1.3.0-beta.13"},{"version":"1.3.0-beta.12"},{"version":"1.3.0-beta.11"},{"version":"1.3.0-beta.10"},{"version":"1.3.0-beta.1"},{"version":"1.3.0"},{"version":"1.2.9"},{"version":"1.2.8"},{"version":"1.2.7"},{"version":"1.2.6"},{"version":"1.2.5"},{"version":"1.2.4"},{"version":"1.2.30"},{"version":"1.2.3"},{"version":"1.2.29"},{"version":"1.2.28"},{"version":"1.2.27"},{"version":"1.2.26"},{"version":"1.2.25"},{"version":"1.2.24"},{"version":"1.2.23"},{"version":"1.2.22"},{"version":"1.2.21"},{"version":"1.2.20"},{"version":"1.2.2"},{"version":"1.2.19"},{"version":"1.2.18"},{"version":"1.2.17"},{"version":"1.2.16"},{"version":"1.2.15"},{"version":"1.2.14"},{"version":"1.2.13"},{"version":"1.2.12"},{"version":"1.2.11"},{"version":"1.2.10"},{"version":"1.2.1"},{"version":"1.2.0rc3"},{"version":
View chainable.js
function chainable(fn) {
console.info('a', this);//window
return function () {
console.info('b1', this);
fn.apply(this, arguments);
console.info('b2', this);
return this;
};
}
View doubleBitwise.js
Math.floor(4.9) === 4 //true
~~4.9 === 4 //true
(4.9 | 0) === 4 //true
View shortFunctionCall.js
function x() {
console.log('x')
};
function y() {
console.log('y')
};
var z = 3;
(z==3?x:y)();
View declareAndCall.js
var func = (function f(a) {
console.log(a);
return f;
})('Hello World');
View Analytics.js
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)