Home >Web Front-end >JS Tutorial >Sharp js function collection_javascript skills

Sharp js function collection_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:51:31964browse

Then share some ideal js prototype functions. Most of the editing and modifications are from Yue Ying's blog
I also recommend Yue Ying's book - "The Return of the King". If you spend more than 5 hours a week on JS coding, it is still worth reading.
1. Function glue. Many students are used to using jq. Sometimes they just use jq for something similar to event = delegate in c#. It seems a bit uneconomical. These prototype functions are enough.

Copy code The code is as follows:

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. Function lingification. Kelingization is an important feature of functional-oriented languages. It is very different from the process-oriented programming ideas held by most people. In my humble opinion, In daily work, except that function coding can make some codes elegant (and perhaps weird), it is not particularly "necessary".
Copy code The code is as follows:

Function.prototype.$curry=function(){
with({that:this})
return function()
{
var args = Array.prototype.slice.call(arguments);
if(args.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. Object closure. This word was coined by me, but you can understand it by looking at the call in the comments. This function was originally intended to prove the equivalence of with and closure, but it provides a very valuable pattern.
Copy code The code is as follows:

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. I am afraid that many js coders want to have a string.Format method in c# (similar to java). In fact, it is not troublesome at all.
Copy code The code is as follows:

String.prototype.$format=function(){
var ret;
for(var i=1;ivar exp = new RegExp('\{' (i-1) '\}','gm' );
ret = (ret||this).replace(exp,arguments[i-1]);
}
return ret;
}
//alert("{0 },{1},{4}".$format(0,1,2));

I will continue to share some such sharp functions when I have the opportunity in the future. Let’s give some functions a name, let’s call them p.js.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn