There is a random hobby such as programming in JavaScript.
I recently had a global question:
How often do JS programmers think about [function statement]?
One of these days I decided to search in the kernel, somehow, it seems,
The book “How to stop programming and start living,” came across.
“It's not about that now” and “No! We will not rely on chance! ”
(© - “Irony of Fate, or“ Enjoy Your Bath ”!”)
What we all know about him:
function My_Function_Name([parametrs]) { [Operators]; }
Almost everyone knows this:
My_Function_Name = function ([parametrs]) { [Operators]; }
Most know about the importance of this:
var My_Function_Name = function ([parametrs]){ [Operators]; }
function () { var My_Function_Name = function ([parametrs]){ [Operators]; } })()
Some special creations even do this:
(function (){ var My_Function_Name = new function ([parametrs]) { [Operators]; } })()
Suppose there is:
A function that does something.
like this:
var mix = function (id){ return document.getElementById(id); }
mix.s = function (id){ mix(id).style; };
I’ll say right away that there will be no questions later:
I know about jQuery, Prototype, Script.Aculo.us, etc.
I understand that it is much more convenient than writing your own.
The meaning of the article is not to write a framework.
We decided to slightly improve the functionality, like this:
mix.T = function(id, par) { if (par) { mix.s(id).top = par; } else { return parseInt(mix.s(id).top); } } mix.L = function(id, par) { if (par) { mix.s(id).left = par; } else { return parseInt(mix.s(id).left); } } mix.W = function(id, par) { if (par) { mix.s(id).width = par; } else { return parseInt(mix.s(id).width); } } mix.H = function(id, par) { if (par) { mix.s(id).height = par; } else { return parseInt(mix.s(id).height); } }
Now let's go back a little, to the nameless functions (closures).
(function() { [Operators]; })()
Ridiculous attempt to make a couple of examples of "on filling":
Personal madness began with the "English Wiki": article
about javascript.
Down there is Syntax and semantics - a great example.
After that, "clicked" known to many
nabla
And, most importantly - the next
thread
Here, they have almost nothing to do with it, but after Vika they were the ones.
From this came out such nonsense, it is obvious that the variables are not completely unnamed:
(function() { mix.init = false; var ag = arguments; //just a sample alert(ag[2]); var prepareFucntionArgs = function(el) { var ra = el; if ((el == "i") || (el == "")) { ra = ["id"]; } if (el == "p") { ra = ["id", "par"]; } return ra; } var prepareFunctionBody = function(el) { var str = el[1] .replace(/_tp/g, "_i _m._a_p _e _r _m._a; }") .replace(/_tr/g, "_i _s._a_p _e _r _I_s._a); }") .replace(/_i/g, "if (par){ ") .replace(/_I/g, " parseInt(") .replace(/_e/g, " }else{ ") .replace(/_r/g, " return ") .replace(/_p/g, " = par; ") .replace(/_m./g, "mix(id).") .replace(/_s./g, "mix.s(id)."); if (el[2]) { str = str.replace(/_a/g, el[2]); } return str; } for (var i in ag[0]) { // parsing "0" arguments array (function() { try { var a = ag[0][i]; var pf_A = prepareFucntionArgs(a[0]); var pf_B = prepareFunctionBody(a); mix[i] = new Function(pf_A, pf_B); } catch (e) { alert(e); alert("" + i + "\n" + pf_A + "\n" + pf_B); } finally {} })(); }; for (var i in ag[1]) { mix[i] = ag[1][i]; }; })({ s: ["", "_r _m.style;"], cn: ["p", "_tp", "className"], T: ["p", "_tr", "top"], L: ["p", "_tr", "left"], W: ["p", "_tr", "width"], H: ["p", "_tr", "height"], In: ["p", "_tp", "innerHTML"] }, { sample: "sample", init: true }, mix = function(id) { return document.getElementById(id); });
Now nothing will save my "roof".
But instead of the superior banalism, there could be someone's polymorphic classes,
neural network scripts and ... DSS, the beginnings of AI. Yeah. ;)
Here's a little bit of code, well, what if someone of a recent type did not know this:
var multiply1 = new Function("x", (function() { return "return x"; })()); alert(multiply1); var multiply2 = eval("new Function(\"x\", \(function(){ return \"return x\"; })()\)"); alert(multiply2); var multiply3 = new Function(["x", "y"], (function() { return "return x*y"; })()); alert(multiply3(5, 6)); var multiply4 = new Function(["x", ["y", "z"]], "return (x*yz); "); alert(multiply4(5, 6, 10));
And finally, a bun that “blew up” the brain: now I understand why function
typeof () constructor function too!
Now it is also clear to me - “why there are still no teleporters”.
It turned out messy, but maybe it will be useful for someone to read.
Source: https://habr.com/ru/post/76377/
All Articles