// 防抖
function debounce(fn, wait) {
var timeout = null;
return function() {
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(fn, wait);
}
}
// 节流throttle代码(定时器):
var throttle = function(func, delay) {
var timer = null;
return function() {
var context = this;
var args = arguments;
if (!timer) {
timer = setTimeout(function() {
func.apply(context, args);
timer = null;
}, delay);
}
}
}
颜色转换
function rgb2hex(sRGB) {
var reg=new RegExp(/^rgb\d1,3,\s∗\d1,3,\s∗\d1,3$/);
if(reg.test(sRGB)){
var num = sRGB.slice(4,-1).split(',');
var res = "#";
for(var i=0;i=0 && num[i]<=255){
num[i]=('0'+parseInt(num[i]).toString(16)).slice(-2);
res += num[i];
}else{
return sRGB;
}
}
return res;
}else{
return sRGB;
}
}
多维数组转一维数组
var arr = ['mu','zi',['dig',['big','love']]]
function flatten(arr){
var res = [];
for(var i=0;i
slice() 从数组中返回选定元素,这里slice()相当于新建一个数组.
高级版: 可以变成 [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1][3, 1, 2][3, 2, 1]
function uniq(array){
var temp = []; //一个新的临时数组
for(var i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1
){
temp.push(array[i]);
}
}
return temp;
}
var str=“They are staudents”;
var str1=“aeiou”;
for(var i=0;i
str=str.split(str1[i]).join('');
}
console.log(str);
如果只是转数字或者什么的 用正则就好a.replace(/[^\d+]/g,"")
或者a.replace(/[^0-9]/g,"")
temp.toString的重写只是为了函数不执行时能够返回最后运算的结果值,所以这个地方是可以任意修改的,你让它返回什么它就返回什么,
var fish = new Fish('小鱼'); fish.run()//undefiend
Fish.prototype = Object.create(Sea.prototype); Fish.prototype.constructor = Fish;
即可输出fish.run(),不直接用Fish.prototype = Sea.prototype;是因为会把Fish的constructor也变成Sea的.代码如下:
function Trim(str)
{
return str.replace(/(^\s*)|(\s*$)/g, "");
}
说明:如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前后所有空格的字符串。
去除全部空格:.replace(/\s/g,"")
var num =0
for(var i=1;i<=inputData;i++){
let count = 0;
let j = i
while( j>0 && j%5 ==0){
j = j/5;
count++
}
num += count
}
process.stdout.write("" + num + "\n");
var arr = [];
var num = 44;
if(num == 1){
arr[0] = 1;
}else{
while(num != 0){
var tmp =num % 2;
arr.push(tmp);
num = (num - tmp) / 2;
}
}
console.log(arr.reverse().join(""));