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
Math.floor(4.9) === 4 //true | |
~~4.9 === 4 //true | |
(4.9 | 0) === 4 //true |
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
var func = (function f(a) { | |
console.log(a); | |
return f; | |
})('Hello World'); |
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(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) |
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
$.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的问题 |
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 onceWraper(func){ | |
return function(){ | |
if(func){ | |
var ret = func.apply(this,arguments); | |
func = null; | |
return ret; | |
}else{ | |
console.log('非第一次运行不再执行'); | |
return; | |
} |
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
//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 = [ |
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 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; |
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
//undefined 与 null 返回false,其他返回true | |
function existy(x) { | |
return x != null; | |
} | |
function truthy(x) { | |
return (x !== false) && existy(x); | |
} | |
function doWhen(condtion, action) { |