Last active
February 12, 2020 10:08
-
-
Save dreamsky/5668683 to your computer and use it in GitHub Desktop.
优秀的 JavaScript 代码片段集合
This file contains hidden or 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 callee 用法示例 | |
function factorial(num) { | |
if (num <= 1) { | |
return 1; | |
} else { | |
return num * arguments.callee(num - 1); | |
} | |
} | |
console.log(factorial(10)); //3628800 |
This file contains hidden or 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 getCity(fn, tryTimes) { | |
var city = $.cookie("city"); | |
if (city) | |
return city; | |
var service = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"; | |
$.getScript(service, function() { | |
var data = remote_ip_info; | |
if (!data) { | |
tryTimes = tryTimes || 0; | |
if (tryTimes < 3) | |
return setTimeout(function() { | |
getCity(fn, ++tryTimes); | |
}, 500); | |
else { | |
return fn(); | |
} | |
} | |
city = data.city ? data.city : (data.province ? data.province : data.country); | |
if (city) { | |
var expiresTime = new Date(); | |
expiresTime.setMinutes(expiresTime.getMinutes() + 30); | |
$.cookie("city", city, { | |
expires: expiresTime, | |
raw: true | |
}); | |
} | |
fn(city); | |
}); | |
} |
This file contains hidden or 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 继成实现常用方式 | |
function ClassA(name){ | |
this.name=name; | |
} | |
ClassA.prototype.sayName=function(){ | |
alert(this.name); | |
} | |
function ClassB(name,age){ | |
ClassA.call(this,name); | |
this.age=age; | |
} | |
ClassB.prototype=new ClassA(); | |
ClassB.prototype.sayAge=function(){ | |
alert(this.age); | |
} |
This file contains hidden or 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 toRGB(hex) { | |
var result = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); | |
if (result) { | |
return "rgb(" + parseInt(result[1], 16) + "," + parseInt(result[2], 16) + "," + parseInt(result[3], 16) + ")"; | |
} else { | |
var shorthandRegex = /^#([a-f\d])([a-f\d])([a-f\d])$/i; | |
return shorthandRegex.test(hex) ? hex : "invalid"; | |
} | |
} | |
// for test | |
console.log(toRGB("#00FFFF")); | |
console.log(toRGB("aaa")); | |
console.log(toRGB("#333")); |
This file contains hidden or 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 数组去重 | |
function unique(arr) { | |
var re = []; | |
var dic = {}; | |
for (var i = 0; i < arr.length; i++) { | |
var item = arr[i]; | |
var key = typeof(item) + item; | |
if (!dic[key]) { | |
re.push(item); | |
dic[key] = true; | |
} | |
} | |
return re; | |
} | |
unique([1, 2, 3, 4, '2', '3', 2, 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment