犀利的js 函數(shù)集合
更新時間:2009年06月11日 22:28:24 作者:
和同事討論js時,我說較為理想的狀態(tài)是,把js當一把好用的匕首,隨手拿來,捅一刀子就走。話雖如此,但現(xiàn)實生活中大部分時候的情況是不理想的。
那么分享幾個理想狀態(tài)的js原型函數(shù)。大部分整理修改自月影的blog
另外推薦一下月影的書——"王者歸來",如果你每周js的coding時間大于5小時,還是值得一讀的。
1.函數(shù)膠水,有很多同學用jq用習慣了,有時就為一個類似于c#里的event+=delegate而用jq,似乎有點劃不來,這幾原型函數(shù)就夠了。
Function.prototype.$concat = function(){
var funcs = [this].concat(Array.apply([], arguments));
return function(){
var ret = [];
for(var i = 0; i < funcs.length; i++){
var func = funcs[i] instanceof Function ? funcs[i] : new Function(funcs[i]);
ret.push(func.apply(this, arguments));
}
return ret;
}
}
//var concat = (function a(a){
// alert("a:"+a);
//}).$concat(function b(b){
// alert("b:"+b);
//});
//concat(1);
2.函數(shù)柯靈化,柯靈化是面向函數(shù)式語言的一個重要特性,和大部分人所持有的面向過程的編程思想?yún)^(qū)別很大,就我愚見,日常工作中,函數(shù)柯靈化除了能把一些代碼寫得優(yōu)雅(或許還有詭異)以外,不是特別的"必要"。
Function.prototype.$curry=function(){
with({that:this})
return function()
{
var args = Array.prototype.slice.call(arguments);
if(args.length<that.length)
{
return function(){
var _args = args.concat(Array.prototype.slice.call(arguments));
return that.$curry().apply(this,_args);
}
}
else return that.apply(this,args);
}
}
//var curry=(function f(a,b,c){
// alert([a,b,c]);
// }).$curry();
//curry(1)(2)(3);
//curry(1,2)(3);
3.對象閉包。這個詞是我造的,不過看一下注釋里的調(diào)用便可以理解,這個函數(shù)原本是為了證明with和閉包的等價性,但卻提供了一個極有價值的模式。
Function.prototype.$bind=function(object){
var callback = function () {
return arguments[0];
}
with(object){
return eval('callback(' + this.toString() + ')');
}
}
//var obj = {a:1,b:2};
//var bind=(function (){
// a=10;
// b=11;
//}).$bind(obj);
//bind();
//alert(obj.a);
4. string.Format。怕是很多js coder都想有一個c#(java類似)里的string.Format方法,其實一點也不麻煩。
String.prototype.$format=function(){
var ret;
for(var i=1;i<arguments.length;i++){
var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
ret = (ret||this).replace(exp,arguments[i-1]);
}
return ret;
}
//alert("{0},{1},{4}".$format(0,1,2));
以后有機會還會陸續(xù)分享一些這樣犀利的函數(shù)。暫且給些函數(shù)合計取個名字,就叫 p.js 吧。
另外推薦一下月影的書——"王者歸來",如果你每周js的coding時間大于5小時,還是值得一讀的。
1.函數(shù)膠水,有很多同學用jq用習慣了,有時就為一個類似于c#里的event+=delegate而用jq,似乎有點劃不來,這幾原型函數(shù)就夠了。
復(fù)制代碼 代碼如下:
Function.prototype.$concat = function(){
var funcs = [this].concat(Array.apply([], arguments));
return function(){
var ret = [];
for(var i = 0; i < funcs.length; i++){
var func = funcs[i] instanceof Function ? funcs[i] : new Function(funcs[i]);
ret.push(func.apply(this, arguments));
}
return ret;
}
}
//var concat = (function a(a){
// alert("a:"+a);
//}).$concat(function b(b){
// alert("b:"+b);
//});
//concat(1);
2.函數(shù)柯靈化,柯靈化是面向函數(shù)式語言的一個重要特性,和大部分人所持有的面向過程的編程思想?yún)^(qū)別很大,就我愚見,日常工作中,函數(shù)柯靈化除了能把一些代碼寫得優(yōu)雅(或許還有詭異)以外,不是特別的"必要"。
復(fù)制代碼 代碼如下:
Function.prototype.$curry=function(){
with({that:this})
return function()
{
var args = Array.prototype.slice.call(arguments);
if(args.length<that.length)
{
return function(){
var _args = args.concat(Array.prototype.slice.call(arguments));
return that.$curry().apply(this,_args);
}
}
else return that.apply(this,args);
}
}
//var curry=(function f(a,b,c){
// alert([a,b,c]);
// }).$curry();
//curry(1)(2)(3);
//curry(1,2)(3);
3.對象閉包。這個詞是我造的,不過看一下注釋里的調(diào)用便可以理解,這個函數(shù)原本是為了證明with和閉包的等價性,但卻提供了一個極有價值的模式。
復(fù)制代碼 代碼如下:
Function.prototype.$bind=function(object){
var callback = function () {
return arguments[0];
}
with(object){
return eval('callback(' + this.toString() + ')');
}
}
//var obj = {a:1,b:2};
//var bind=(function (){
// a=10;
// b=11;
//}).$bind(obj);
//bind();
//alert(obj.a);
4. string.Format。怕是很多js coder都想有一個c#(java類似)里的string.Format方法,其實一點也不麻煩。
復(fù)制代碼 代碼如下:
String.prototype.$format=function(){
var ret;
for(var i=1;i<arguments.length;i++){
var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
ret = (ret||this).replace(exp,arguments[i-1]);
}
return ret;
}
//alert("{0},{1},{4}".$format(0,1,2));
以后有機會還會陸續(xù)分享一些這樣犀利的函數(shù)。暫且給些函數(shù)合計取個名字,就叫 p.js 吧。
相關(guān)文章
JavaScript中關(guān)于數(shù)組的調(diào)用方式
這篇文章主要介紹了JavaScript中關(guān)于數(shù)組的調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
JS的時間格式化和時間戳轉(zhuǎn)換函數(shù)示例詳解
這篇文章主要介紹了JS的時間格式化和時間戳轉(zhuǎn)換函數(shù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
javascript實現(xiàn)uploadify上傳格式以及個數(shù)限制
這篇文章主要介紹了javascript如何限制uploadify上傳格式以及個數(shù)的實現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-11-11
JS實現(xiàn)跟隨鼠標閃爍轉(zhuǎn)動色塊的方法
這篇文章主要介紹了JS實現(xiàn)跟隨鼠標閃爍轉(zhuǎn)動色塊的方法,涉及javascript操作html元素及css樣式的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02
pnpm?monorepo?聯(lián)調(diào)方案問題解析
文章主要介紹了在pnpmmonorepo環(huán)境下進行多庫聯(lián)調(diào)的方案,包括使用`pnpmlink`命令來鏈接指定的文件夾或全局的`node_modules`,并在項目中通過`pnpmlink--global<pkg>`來引用這些庫,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-12-12

