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)
View debounce&throttle.js
$.debounce = function(fn, time, ctx) {
var ret = 0;
if( typeof time !== 'number' ) {
ctx = time;
time = 50;
}
time = time || 50;
return function() {
var args = [].slice.apply(arguments);
// 注意是ctx的问题
View shareFunc.js
function shareFunc(func){
//初始化函数运行过程中依赖的变量
//之前一般的方式是声明全局变量,程序的阅读与理解难度增加,全局变量容易受干扰
var varWraper ={
a:0
}
return function(){
var args = [].slice.apply(arguments);
args.push(varWraper);
var ret = func.apply(this,args);
View onceWraper.js
function onceWraper(func){
return function(){
if(func){
var ret = func.apply(this,arguments);
func = null;
return ret;
}else{
console.log('非第一次运行不再执行');
return;
}
View reduce.js
//1.数组求和
var arr = [1,2,3,4];
var total = arr.reduce(function(pre, cur, index, arr){
//接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce 的数组
//arr.reduce(callback,[initialValue])
return pre + cur;
})
console.log(total)
//2.把二维数组转换为json键值对
var relArray = [
View curry.js
function Foo(x, y, z, w) {
var args = arguments;
if (args.length < Foo.length) {
return function() {
var oldArgs = Array.prototype.slice.call(args);
var newArgs = oldArgs.concat(Array.prototype.slice.call(arguments));
return args.callee.apply(null,newArgs);
}
} else {
return x + y - z * w;
View truthy.js
//undefined 与 null 返回false,其他返回true
function existy(x) {
return x != null;
}
function truthy(x) {
return (x !== false) && existy(x);
}
function doWhen(condtion, action) {