View strlen.js
// 返回字符串的实际长度, 一个汉字算2个长度
String.prototype.strlen = function () {
return this.replace(/[^\x00-\xff]/g, "**").length;
}
View ~~.js
// ~符号用在JavaScript中有按位取反的作用,~~即是取反两次,而位运算的操作值要求是整数,其结果也是整数,所以经过位运算的都会自动变成整数,可以巧妙的去掉小数部分,类似于parseInt,比如:
let a = 1.23;
let b = -1.23;
console.log(~~a); // 1
console.log(~~b); // -1
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 array-max.js
var arry = [1,2,3];
Math.max.apply(null,arry);//得到的结果会是3;
Math.min.apply(null,arry);//得到的结果会是1;
View array-reduce.js
// reduce方法
let arr = [1, 2, 3, 4, 5, 6];
let reduceResult = arr.reduce((x, y) => {
return x + y
}, 123);
console.log(reduceResult);
View array-unique.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 cmp.js
//老记混顺序,特此整理一下
function cmp(a, b){
//a 在前-1,a 在后1
if(a<b) return 1; //a小于b的情况下,a在后
if(a>b) return -1;//a大于b的情况下,a在前
return 0;
}
var a=[1,10,5,9,2];
console.log(a.sort(cmp));
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;
};
}