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 debounce(fn, delay) { | |
var timeout; | |
return function() { | |
var _this = this; | |
var _arguements = arguments; | |
clearTimeout(timeout); | |
timeout = setTimeout(function() { | |
fn.apply(_this, _arguements); | |
}, delay); |
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
setTimeout(function() { | |
console.log(1) | |
}, 0); | |
new Promise(function(a, b) { | |
console.log(2); | |
for(var i = 0; i < 1000000; i++) { | |
i == 999999 && a(); | |
} | |
console.log(3); | |
}).then(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 sleep(milliseconds) { | |
var start = new Date().getTime(); | |
for (var i = 0; i < 1e7; i++) { | |
if ((new Date().getTime() - start) > milliseconds){ | |
break; | |
} | |
} | |
} | |
const start = +new Date; | |
console.log('Dude!'); |
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 createPet = function(name) { | |
var sex; | |
return { | |
setName: function(newName) { | |
name = newName; | |
}, | |
getName: function() { | |
return 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 getPrototypeChain(object) { | |
var protoChain = []; | |
while (object = object.__proto__) { | |
protoChain.push(object); | |
} | |
protoChain.push(null); | |
return protoChain; | |
} |
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 printing() { | |
console.log(1); | |
setTimeout(function() { console.log(2); }, 1000); | |
setTimeout(function() { console.log(3); }, 0); | |
console.log(4); | |
} |
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 lang = function(){}; | |
lang.extend = lang.prototype.extend = function(){ | |
var len = arguments.length, | |
target = {}, | |
options, name, src, copy, clone, i= 0; | |
if (len === 1) { | |
target = this; | |
} else if(len > 1) { |
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 getElementsByClassName(clsName, element) { | |
var arr = []; | |
var aEle = element.getElementsByTagName('*'); | |
var re = new RegExp('\\b'+ clsName +'\\b'); | |
for (var i= 0,len=aEle.length; i<len; i++) { | |
if (re.test(aEle[i].className)) { | |
arr.push(aEle[i]); |
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
/* 有一个数n,不用for/while循环,怎么返回[1,2,3,...,n]这样一个数组 */ | |
// 方法一:递归 | |
function recursionLoop(n) { | |
var arr = []; | |
return (function () { | |
arr.unshift(n); // 入队 | |
n--; | |
if (n != 0) { |
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
/* a,b两个变量,不用第三个变量来交换两个变量的值 */ | |
// a,b为数字 | |
var a = 1, | |
b = 2; | |
a = a + b; | |
b = a - b; | |
a = a - b; | |
NewerOlder