Rumah >pembangunan bahagian belakang >tutorial php >C++、PHP、Javascript、...、对lambda表达式的支持
lambda
lambda表达式,也叫Closure(闭包),也叫匿名函数。由于其强大之处,几乎被所有主流开发语言支持,本文试图列出大部分语言中lambda表达式的样例代码,后续持续更新。
PHP对lambda的支持
<code><span><?php </span><span>$i</span> = <span>12</span>; <span>$j</span> = <span>33</span>; <span>$callable</span> = <span><span>function</span><span>()</span><span>use</span><span>(<span>$i</span>, &<span>$j</span>)</span> {</span><span>echo</span><span>$i</span> . <span>"\n"</span>; <span>echo</span><span>$j</span> . <span>"\n"</span>; }; <span>$callable</span>(); <span>$i</span>++; <span>$j</span>++; <span>$callable</span>(); </span></code>
C++对lambda的支持
<code><span>#include <iostream></iostream></span><span>using</span><span>namespace</span><span>std</span>; <span>int</span> main(<span>int</span> argc, <span>char</span>** argv) { <span>int</span> i = <span>12</span>; <span>int</span> j = <span>33</span>; <span>auto</span> callable = [i, &j](){ <span>cout</span> cout </code>
[=][&]
等简易语法来引用全部外部变量。Javascript
<code><script> var <span>i = <span>12; var <span>j = <span>33; var callable = <span><span>function<span>(){ alert(<span>i); alert(<span>j); } callable(); <span>i++; <span>j++; callable(); </script></code>
以上就介绍了C++、PHP、Javascript、...、对lambda表达式的支持,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。