//*** 1j *** function outer(x) // { var y=2; // function inner(a) // { var b=4; // /* * , * */ var res=x+y+a+b; alert(res); // 10 . } inner(3); // } outer(1); // , .
// *** 2j *** adder=function(a,b) // , { return a+b; // } subber=function(a,b) { return ab; } // , /* * - adder subber * , , , * . .. adder subber , , * : x=adder(1,3); x . */ function performAction(action, a, b) // performAction { var result=action(a,b); // action - return result; } function makeDivider() // makeDivider { return function (a,b) // { return a/b; } } r1=adder(1,2); // . r1=1+2; r2=performAction(subber,6,4); // . r2=6-4; r3=performAction(function(a,b) {return a*b;} ,5,6); // . r3=5*6; divider=makeDivider(); // , , r4=divider(16,4); // : r4=16/4; r5=makeDivider()(32,16);// , : r5=32/16; alert([r1,r2,r3,r4,r5]); //3,2,30,4,2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Closure test</title> </head> <body> <a href="#" id="link1"></a> <a href="#" id="link2"> </a> <div id="hide1"> </div> <div id="hide2"> </div> <script> // *** 3j *** function getHider(id) // id { return function() { document.getElementById(id).style.display='none'; return false; } /* * , id * . * getHider * , * . */ } document.getElementById('link1').onclick=getHider('hide1'); document.getElementById('link2').onclick=getHider('hide2'); /* * onclick * getHider, , * , id='hide1' * id='hide2' . , * , */ </script> </body> </html>
// *** 1p *** function outer($x) // { $y=2; // $inner=function ($a) use ($x, $y) // { $b=4; // /* * , * */ $res=$x+$y+$a+$b; echo $res; // 10 . }; $inner(3); // } outer(1);
// *** 2p *** $adder=function($a,$b) // , { return $a+$b; // }; $subber=function($a,$b) { return $a-$b; }; // , function performAction($action, $a, $b) // performAction { $result=$action($a,$b); // action - return $result; } function makeDivider() // makeDivider { return function ($a,$b) // { return $a/$b; }; } $r1=$adder(1,2); // . r1=1+2; $r2=performAction($subber,6,4); // . r2=6-4; $r3=performAction(function($a,$b) {return $a*$b;} ,5,6); // . r3=5*6; $divider=makeDivider(); // , , $r4=$divider(16,4); // : r4=16/4; // r5 PHP . //$r5=makeDivider()(32,16);// , : r5=32/16; $r5='php fail'; echo "$r1,$r2,$r3,$r4,$r5"; //3,2,30,4,php fail
// : function cmp($a, $b) { return($a > $b); } // uasort($array, 'cmp'); // // : uasort($array, function($a, $b) { return($a > $b);});
// , PHP 4!: uasort($array, create_function('$a, $b','return $a > $b;'));
function getModernIncrementer() { $x=0; return function() use(&$x) // ! { return $x++; }; } $incrementer2=getModernIncrementer(); echo $incrementer2(), $incrementer2(), $incrementer2();//012
function incrementer() { static $x=0; return $x++; } echo incrementer(),incrementer(),incrementer(); //012
var_dump(function(){return 1;}); // object(Closure)#1 (0) { }
class Test { public function __invoke () { return 123; } } $func = new Test(); echo $func(); //123
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>dynaedit</title> <style type="text/css"> .msg, .edt {font-family: "Verdana", "Arial", "Helvetica", sans-serif; font-size: 10pt} .msg {background-color:#DDf; border: 1px solid #000; padding:2px} .edt {margin:-2px 0 -2px -1px; padding:0; width:100%; height:100%; border 0} </style> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> $(document).ready(function() { $("#admin").click(function() { $(this).remove(); $("p.msg").each(function() { var msg_id=this.id.substr(1); // this? each! var msg=$(this); // JQuery DOM <p> $("<input/>", // { type: "button", value: " #"+msg_id, click: function StartEdit() // p textarea { var edt=$("<textarea/>", // textarea, p { 'class':'edt', value:msg.html().replace(/<br[^>]*>/gim,"\n"), // msg? => height:msg.height(), }).appendTo(msg.empty()); $(this).val(" #"+msg_id).unbind().click(function() // { //$.post("/ajax/savemessage",{msg_id:msg_id, msg:edt.val()}, function(data){}); // msg.html(edt.remove().val().replace(/\n/gm,"<br />")); // textarea, $(this).val(" #"+msg_id).unbind().click(StartEdit);// , return false; });//Save return false; } //StartEdit() }).insertAfter(this);//<input/> });//$("p.msg").each return false; });//$("#admin").click });//$(document).ready </script> </head> <body> <p id="p1234" class="msg"> <br /> !</p> <p id="p1235" class="msg"> <br /> !<br />PS Just 4 lulz</p> <p><a href="#" id="admin"> !</a></p> </body> </html>
Source: https://habr.com/ru/post/103983/
All Articles