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
// ~符号用在JavaScript中有按位取反的作用,~~即是取反两次,而位运算的操作值要求是整数,其结果也是整数,所以经过位运算的都会自动变成整数,可以巧妙的去掉小数部分,类似于parseInt,比如: | |
let a = 1.23; | |
let b = -1.23; | |
console.log(~~a); // 1 | |
console.log(~~b); // -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
let CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789-_.~!*()'; // except \' | |
function decToRadixN(radixChars, numLen) { | |
let result = []; | |
let radix = radixChars.length; | |
return function cacl(dec) { | |
result.unshift(dec % radix); | |
if (dec >= radix) { | |
dec = Math.floor(dec / radix); | |
return cacl(dec, radix); | |
} |
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 getThirdResoure() { | |
var resp = null; | |
var tags = document.querySelectorAll('iframe[src],frame[src],script[src],link[rel=stylesheet],object[data],embed[src]'); | |
for(var i = 0; i < tags.length; i++){ | |
var tag = tags[i]; | |
var a = document.createElement('a'); | |
a.href = tag.src||tag.href||tag.data; | |
if(a.hostname != location.hostname){ | |
resp = resp || []; | |
resp.push({name: tag.localName, href: a.href}); |
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 toUnicodeSequence(str) { | |
for(var i = str.length; i--;) { | |
str = str.slice(0, i) + '\\u' | |
+ ('000' + str.charCodeAt(i).toString(16)).slice(-4) | |
+ str.slice(i + 1); | |
} | |
return str; | |
} |
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
let 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
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() { //借用对象 |
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
// reduce方法 | |
let arr = [1, 2, 3, 4, 5, 6]; | |
let reduceResult = arr.reduce((x, y) => { | |
return x + y | |
}, 123); | |
console.log(reduceResult); |
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
// 返回字符串的实际长度, 一个汉字算2个长度 | |
String.prototype.strlen = function () { | |
return this.replace(/[^\x00-\xff]/g, "**").length; | |
} |
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
let fs = require('fs'); | |
let crypto = require('crypto'); | |
let path = '/Users/lushijie/Pictures/day1.jpg'; | |
let start = new Date().getTime(); | |
let md5sum = crypto.createHash('md5'); | |
let stream = fs.createReadStream(path); | |
stream.on('data', function(chunk) { | |
md5sum.update(chunk); | |
}); |
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
// http://lib.baomitu.com/crypto-js/3.1.9-1/crypto-js.js | |
function encrypt(content, key) { | |
var encryptResult = CryptoJS.AES.encrypt( | |
content, | |
key, | |
{ | |
//iv: key, | |
mode: CryptoJS.mode.ECB, | |
padding: CryptoJS.pad.Pkcs7 | |
} |