JavaScript code (simplified version) _ javascript skills similar to Lambda Expression Programming

Source: Internet
Author: User
In my previous blog post, I introduced a method to make JavaScript have a programming capability similar to Lambda expressions-but it has some disadvantages, the most important obstacle to its use is the addition of parentheses, making the code hard to read. After posting a blog post, I spent some time trying to solve this problem ...... After several attempts, I found another pattern, which is no longer necessary:

The Code is as follows:


Eval (function (){
Var s = '', ww = [v]> (s + = v );
Var ws = [n]> ww ('+ n + ')');
PnView3 (14, [n]> ww ('[' + n + ']'),
1, 37,
Ws, []> ww ('...'),
2, 1
);
Document. write (s );
}. Lamda0 ());


However, due to the operator priority, comparison, gate, and assignment operators cannot be directly written in (pseudo) Lambda expressions.
That is to say

The Code is as follows:


Function (a, B) {a = B}


Still need to write

The Code is as follows:


[A, B]> (a = B)


In addition, the selected pattern itself has actual results-when comparing an array with another thing, the script engine first tries to convert both sides into values, if it fails, it is converted into a string and then compared.
However, I think under normal circumstances, few people will compare the array with other things-so they do not even need to take the initiative to avoid it, as long as they are not used (pseudo) if Lambda expressions are not used in this way, it will be okay.

The new implementation code is as follows:

The Code is as follows:


/*!
L-amda "a-Lambda", a module provides Alternate "Lambda" style programming ability for JavaScript.
Created By NanaLich. 2010-09-10
This module is published under WTFPL v2, so you just do what the Fxxx you want to with it.
*/
! Function (){
Function attachEntry (o, a, m ){
Var I, j, n;
O = []. concat (o );
While (I = o. shift ()){
For (j in ){
If (! I [n = a [j]) I [n] = m;
}
}
}
Var xx = /"(? : \ [\ S \ S] | [^ \ x22]) * "| '(? : \ [\ S \ S] | [^ \ x27]) * '| ([^ \ s \ w] \ s *) \ [(\ s * | \ s * [A-Z $ _] [\ w $] * \ s *(? :, \ S * [A-Z $ _] [\ w $] * \ s *) \] \ s * (>) \ s *(\(?) /Gi;
Var xy =/[\ n \ r),; \]}] | $/. source;
Function rxClone (rx ){
Return new RegExp (rx. source, (rx. global? 'G': '') + (rx. ignoreCase? 'I': '') + (rx. multiline? 'M ':''));
}
AttachEntry (RegExp, ['clone '], rxClone );
AttachEntry (RegExp. prototype, ['clone '], function () {return rxClone (this );});
Function translateLambda (s ){
Var m, l = 0, r = '', x = xx. clone (); // because firefox, safari, and other browsers have over-optimized global matching regular expressions, a roundabout method is used to create non-repeated Regular Expression instances.
While (m = x.exe c (s )){
Var h = m [0];
Switch (h. charAt (0) {// determine the expected syntax
Case '$': // parameter passing by function
Case ')':
Case ']':
Case '"': // matched string
Case "'":
Continue; // skip all the above steps
}
Var p, q, t, k = m [4]. length, y = new RegExp (k? '\)': Xy, 'G ');
R + = s. substring (l, p = m. index); // Add the remaining content to the result string.
Y. lastIndex = l = p + h. length; // start from the pseudo operator to find the right parenthesis or other symbols
While (q = y.exe c (s )){
Q = q. index;
Try {
T = 'Return ('+ s. substring (l, q) + ');';
New Function (t); // syntax Test
R + = m [1] + 'function ('+ m [2] +') {'+ translateLambda (t) +'} '; // content in the Translation
X. lastIndex = l = q + k; // The next query starts after the current boundary.
Break;
} Catch (ex ){}
}
If (! Q) l = p; // It indicates that no right brackets or valid code can be found, and all matching content can be appended directly.
}
Try {
R + = s. substr (l );
New Function (r); // syntax Test
Return r;
} Catch (ex) {// failed. The original text is returned.
Return s;
}
};
Var lamdaAliases = ["translatelames", "lambda", "lamda"];
AttachEntry (String, lamdaAliases, translatelames );
AttachEntry (String. prototype, lamdaAliases, function () {return translatelames (this );});
Var funPrototype = Function. prototype;
AttachEntry (Function, lamdaAliases, function (func) {return translateLambda ('0, '+ func );});
AttachEntry (funPrototype, lamdaAliases, function () {return translatelames ('0, '+ this );});
Var lamda0aliases = ['lambdainit', 'lambda0', 'lamda0'];
AttachEntry (Function, lamda0aliases, function (func) {return translatelames ('! '+ Func + '()');});
AttachEntry (funPrototype, lamda0aliases, function () {return translatelames ('! '+ This + '()');});
}();


This time, we added a special method for the function, removed the complete judgment, and added a new method to slightly simplify the call process;
Fixed the BUG where the expected syntax component could not be determined when extra spaces exist.

In addition, since Codeplex went crazy again, it was not downloaded this time.
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.