标签: anonymous-function

JavaScript 中的类变量和 setInterval

由于我需要将匿名函数传递给setInterval我是否需要参数,因此我尝试使用以下代码。最初我让它调用this.countUp,但是当它返回时,NaN我做了一些阅读并.call(this)在 SO 上找到了解决方案。然而,当我将它与匿名函数(我承认我有点迷茫)结合起来时,我现在得到了TypeError: this.countUp is undefined了 Firebug。

我想我不需要使count可访问性,也不需要playBeep方法,但让我们假装我想要这样我就可以理解我在这段代码中做错了什么。

    function workout() {
        var beep = new Audio("beep1.wav");
        this.timerWorkout; //three timers in object scope so I can clear later from a different method
        this.timerCounter; 
        this.timerCoolDown;
        this.count = 0;

        this.startWorkout = function() {
            alert(this.count);
            this.timerWorkout = setTimeout(this.playBeep, 30 * 1000); //workout beep - 30 seconds
            this.timerCounter = setInterval(function() {this.countUp.call(this)}, 1000); //on screen timer - every second

        }

        this.startCoolDown = …
Run Code Online (Sandbox Code Playgroud)

javascript timer object anonymous-function setinterval

1
推荐指数
1
解决办法
1555
查看次数

递归匿名函数计算序列总和

我尝试编写一个函数来计算以下内容, 在此输入图像描述

我能想出的就是这个,这是行不通的。

$fact = sub {
    $n = shift;
    if($n==0 || $n ==1){
        return 1;
    }else{
        return $n*&$fact($n-1);
    }
}

sub fun{
    ($x,$n)= @_;
    if($n==0){
        return 1;
    }elsif($n == 1){
        return $x;
    }else{
        return ($x)/&$fact($n)+fun($x,$n-1);
    }
}



print (fun(3,5));
Run Code Online (Sandbox Code Playgroud)

recursion perl anonymous-function

1
推荐指数
1
解决办法
359
查看次数

Scala 中自定义类型的一元运算符定义

尝试运行此代码:

def ! : Int => Boolean = (p : Int => Boolean) => !p

有编译错误:

[error] value unary_! is not a member of Int => Boolean  
[error]   def ! : Int => Boolean = (p : Int => Boolean) => !p  
Run Code Online (Sandbox Code Playgroud)

错误突出显示为“!p”

编译器不应该自动计算出 p 的结果是 aBoolean吗?

提前致谢

编辑:根据评论,也尝试了以下内容。使用其他方法完成了我的任务,但我正在尝试学习如何定义一元运算符

def unary_! : Int => Boolean = (p : Int => Boolean) => !(p(_))

仍然收到编译器错误 "!(p(_))"

scala operator-overloading anonymous-function

1
推荐指数
1
解决办法
2875
查看次数

匿名函数的作用域

我写了一个带有匿名函数回调的路由器。

样本

$this->getRouter()->addRoute('/login', function() {
    Controller::get('login.php', $this);
});

$this->getRouter()->addRoute('^/activate/([a-zA-Z0-9\-]+)$', function($token) {
    Controller::get('activate.php', $this);
});
Run Code Online (Sandbox Code Playgroud)

对于较小的代码,我想将它移动到一个数组。

我用以下方法编写了一个路由类:

<?php
    namespace CTN;

    class Routing {
        private $path           = '/';
        private $controller     = NULL;

        public function __construct($path, $controller = NULL) {
            $this->path         = $path;
            $this->controller   = $controller;
        }

        public function getPath() {
            return $this->path;
        }

        public function hasController() {
            return !($this->controller === NULL);
        }

        public function getController() {
            return $this->controller;
        }
    }
?>
Run Code Online (Sandbox Code Playgroud)

我的数组具有新类的路由路径:

foreach([
    new Routing('/login', 'login.php'),
    new Routing('^/activate/([a-zA-Z0-9\-]+)$', 'activate.php');
] AS $routing) …
Run Code Online (Sandbox Code Playgroud)

php scope anonymous-function

1
推荐指数
1
解决办法
72
查看次数

我们可以省略 JavaScript IIFE 的主括号吗?

省略 JavaScript IIFE 的主括号(...)并仅();在末尾以及函数表达式分配给变量时使用是否有任何错误(或不良实践)?

let foo = function() {
  return 'Hello'
}();

// so we use 'foo', not 'foo()'
console.log(foo); // 'Hello'
Run Code Online (Sandbox Code Playgroud)

虽然TypeScript在没有警告的情况下接受这一点,但我们应该总是这样做吗?

let foo = (function() {
  return 'Hello'
})();
Run Code Online (Sandbox Code Playgroud)

javascript function anonymous-function

1
推荐指数
1
解决办法
122
查看次数

匿名函数是否支持可选参数?

有没有办法在 MATLAB 中实现的匿名函数中使用可选参数?

请参阅以下示例:

foo = @(x,y)(x+y+12)
Run Code Online (Sandbox Code Playgroud)

可以y是上述匿名函数中的可选参数,例如

foo = @(x,y?)(x+y+12)
Run Code Online (Sandbox Code Playgroud)

并且仅y在提供时使用?

matlab anonymous-function optional-parameters

1
推荐指数
1
解决办法
75
查看次数

在 golang 中返回递归匿名函数

我希望能够在 golang 中返回一个递归匿名函数。我使用了下面的代码片段。此处 foo() 不起作用,因为匿名函数无法引用自身。bar() 按预期工作。

如果可能的话,这样做的正确方法是什么?

package main

import (
    "fmt"
)

func foo() func(int) int {
    return func(x int) int {
        if x == 1 {
            return 1
        }
        return x * func(x-1) // this is where the problem lies
    }
}
func bar() func(int) int {
    return func(x int) int {
        return x * 100 
    }
}

func main() {

    a:= foo()
    b:= bar()
    fmt.Println(a(5))
    fmt.Println(b(5))

}
Run Code Online (Sandbox Code Playgroud)

anonymous-function go

1
推荐指数
1
解决办法
78
查看次数

当只有一个元素时,数组中的每个元素都会同时递增

我使用以下代码来增加围绕给定元素的2d数组中的元素.

 EmptyCell = {number: 0}; //This has several parts in the actual code.
 list = new Array();

function init(w,h){
    for (var x = 0; x <= w; x++){
        list[x] = new Array();
        for (var y = 0 ; y <= h; y++){
            list[x][y] = EmptyCell;
        }
    }
}

function map(func,x,y){
    var xoff = [1,1,1,0,0,-1,-1,-1];
    var yoff = [1,0,-1,1,-1,1,0,-1];
    for (var atIndex = 0; atIndex < 8; atIndex++){
        func(x+xoff[atIndex],y+yoff[atIndex]);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我像这样运行它:

init(10,10);

map(function(x,y){
    if (list[x] != null && list[x][y] != …
Run Code Online (Sandbox Code Playgroud)

javascript arrays anonymous-function

0
推荐指数
1
解决办法
474
查看次数

当没有包装在jquery中的匿名函数时,代码无法运行

如果您在http://htmledit.squarefree.com/中粘贴这些代码http://paste.plurk.com/show/152772

您将看到代码运行没有任何问题.这些图像一共会变成幻灯片.

但是,如果您粘贴以下代码:

http://paste.plurk.com/show/152773

代码将无法运行,无法播放幻灯片.

这两段代码只是代码是否包含在jquery匿名函数中.

我只是不知道为什么第二段代码不起作用.

javascript jquery anonymous-function

0
推荐指数
1
解决办法
121
查看次数

PHP Lambda函数

嘿家伙我搞砸了我的Lambda,似乎我的匿名函数没有得到上面的变量,

进入的一些变革是

print_r($cacheTypes); 
print_r($servers); 

Array
(
    [concreter] => on
    [config] => on
)
Array
(
    [0] => dev-www.domain.com
)
Run Code Online (Sandbox Code Playgroud)

功能是

$urls = array_walk($servers, 
    create_function('&$n', 
        '$n = "http://{$server}/".($vcpParam 
            ? "flush-file-cache" 
            : "flushFileCache.php"
        )."?tags=".implode("-", array_keys($cacheTypes));'
    )
);
Run Code Online (Sandbox Code Playgroud)

错误是

Warning: array_keys() expects parameter 1 to be array, null given
Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed
Run Code Online (Sandbox Code Playgroud)

非常感谢.我很肯定它没有认识到我正在输入的变量,但我不确定为什么

php lambda anonymous-function

0
推荐指数
1
解决办法
1001
查看次数