Sharp js function set

Source: Internet
Author: User

Then we will share several js prototype functions in ideal states. Mostly organize and modify the blog from the shadow
In addition, we recommend that you read the shadow book "Return of the King". If your js coding time is more than 5 hours a week, it is worth reading.
1. function Glue, many people get used to using jq. Sometimes jq is used for an event + = delegate similar to c #. It seems that this prototype function is enough.
Copy codeThe 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 (){
// Alert ("a:" + );
//}). $ Concat (function B (B ){
// Alert ("B:" + B );
//});
// Concat (1 );

2. function ke linghua and Ke linghua are an important feature of function-oriented languages. They are very different from the process-oriented programming ideas held by most people. I am confused. In my daily work, in addition to being able to write some code elegantly (or maybe weird), function KE Ling is not particularly "necessary ".
Copy codeThe Code is as follows:
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. Object closure. I made the word, but I can understand the call in the comment. This function was originally designed to prove the equivalence between with and closure, but provided a very valuable model.
Copy codeThe 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. );

4. string. Format. Even if many js coder want to have a string. Format method in c # (similar to java), it is not troublesome.
Copy codeThe Code is as follows:
String. prototype. $ format = function (){
Var ret;
For (var I = 1; I <arguments. length; I ++ ){
Var exp = new RegExp ('\ {' + (I-1) + '\}', 'G ');
Ret = (ret | this). replace (exp, arguments [I-1]);
}
Return ret;
}
// Alert ("{0}, {1}, {4}". $ format (0, 1, 2 ));

In the future, we will have the opportunity to share some of these sharp functions. For the moment, let's name the total number of functions, namely p. js.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.