function doSomethingWith(param)
{
document.body.addEventListener(
'scroll',
function()
{
document.write(param);
},
false
); // An event that I want to remove later
}
setTimeout(
function()
{
document.body.removeEventListener('scroll', HANDLER ,false);
// What HANDLER should I specify to remove the anonymous handler above?
},
3000
);
doSomethingWith('Test. ');
Run Code Online (Sandbox Code Playgroud) PHP手册说明
$this在PHP 5.4.0之前无法使用匿名函数
在匿名功能页面上.但我发现我可以通过赋值$this给变量并将变量传递给use函数定义中的语句来使其工作.
$CI = $this;
$callback = function () use ($CI) {
$CI->public_method();
};
Run Code Online (Sandbox Code Playgroud)
这是一个好习惯吗?
有没有更好的方法来$this使用PHP 5.3 访问匿名函数?
使用短符号#(..)有一些我不了解匿名函数的东西
以下作品:
REPL> ((fn [s] s) "Eh")
"Eh"
Run Code Online (Sandbox Code Playgroud)
但这不是:
REPL> (#(%) "Eh")
Run Code Online (Sandbox Code Playgroud)
这有效:
REPL> (#(str %) "Eh")
"Eh"
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么(#(%)"Eh")不起作用,同时我不需要使用str in ((fn [s] s)"Eh")
它们都是匿名函数,它们都带有一个参数.为什么简写符号需要一个函数而另一个符号不需要?
我非常兴奋地阅读php中的匿名函数,它允许您声明一个比使用create_function更容易的函数变量.现在我想知道我是否有一个传递变量的函数,我如何检查它以确定它是否是一个函数?还没有is_function()函数,当我做一个函数的变量var_dump时::
$func = function(){
echo 'asdf';
};
var_dump($func);
Run Code Online (Sandbox Code Playgroud)
我明白了:
object(Closure)#8 (0) { }
Run Code Online (Sandbox Code Playgroud)
有关如何检查这是否是一个功能的任何想法?
在严格模式下使用javascript时,为什么这个匿名函数未定义?我理解为什么这可能有意义,但我找不到任何具体的答案.
例:
(function () {
"use strict";
this.foo = "bar"; // *this* is undefined, why?
}());
Run Code Online (Sandbox Code Playgroud)
小提琴测试:http://jsfiddle.net/Pyr5g/1/ 查看记录器(firebug).
class MyClass {
var $lambda;
function __construct() {
$this->lambda = function() {echo 'hello world';};
// no errors here, so I assume that this is legal
}
}
$myInstance = new MyClass();
$myInstance->lambda();
//Fatal error: Call to undefined method MyClass::lambda()
Run Code Online (Sandbox Code Playgroud)
那么到达类变量的正确语法是什么?
我一直在研究PHP中的闭包,这引起了我的注意:
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
Run Code Online (Sandbox Code Playgroud)
有人请给我一个关于use此代码中使用情况的解释.
function ($quantity, $product) use ($tax, &$total)
Run Code Online (Sandbox Code Playgroud)
当我use在PHP中搜索时,它会找到use在命名空间中使用的关键字,但在这里它看起来不同.
谢谢.
可能的重复:
什么是PHP或Javascript中的Closures/Lambda外行术语?
'闭包'和'lambda'有什么区别?
嗨,
我一直无法找到一个明确解释闭包和匿名函数之间差异的定义.
我看到的大多数参考文献清楚地指出它们是不同的"事物",但我似乎无法理解为什么.
有人可以帮我简化一下吗?这两种语言功能之间有哪些具体差异?在哪些情况下哪一个更合适?
我知道这很愚蠢,但这有什么区别:
(function() {
var foo = 'bar';
})();
Run Code Online (Sandbox Code Playgroud)
还有这个?
(function() {
var foo = 'bar';
}());
Run Code Online (Sandbox Code Playgroud)
JSLint告诉我们Move the invocation into the parens that contain the function,但我认为没有必要.
编辑:答案太酷了.~function,JSHint的替代方案以及jQuery对(/***/)();Crockford的解释的偏好!我以为我会得到一个"他们是同一件事"的答案.
你们通过upvotes决定最好的一个,我勾选它.
function foo(x) {
console.log(arguments)
} //foo(1) prints [1]
Run Code Online (Sandbox Code Playgroud)
但
var bar = x => console.log(arguments)
Run Code Online (Sandbox Code Playgroud)
以相同方式调用时出现以下错误:
Uncaught ReferenceError: arguments is not defined
Run Code Online (Sandbox Code Playgroud) closures ×4
javascript ×4
php ×4
lambda ×2
arguments ×1
clojure ×1
comparison ×1
ecmascript-6 ×1
function ×1
jslint ×1
use-strict ×1