标签: anonymous-function

将FieldName作为参数传递

我不太确定我正在尝试做什么被称为,所以我很难找到谷歌的任何线索.

我有几个具有相同逻辑的方法,唯一不同的是它们在对象上使用的属性.

class Foo
{
   public int A(Bar bar)
   {
      return bar.A * 2;
   }

   public int B(Bar bar)
   {
      return bar.B * 2;
   }

   public int C(Bar bar)
   {
      return bar.C * 2;
   }
}

class Bar
{
   public int A;
   public int B;
   public int C;
}
Run Code Online (Sandbox Code Playgroud)

而不是Foo我想要的三个单独的方法,具有看起来更像的签名

public int X(Bar bar, ??? x)
{
   return bar.x * 2;
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

c# reflection anonymous-function

4
推荐指数
2
解决办法
9152
查看次数

在php中调用匿名函数定义为对象变量

我有PHP代码,如:

class Foo {
  public $anonFunction;
  public function __construct() {
    $this->anonFunction = function() {
      echo "called";
    }
  }
}

$foo = new Foo();
//First method
$bar = $foo->anonFunction();
$bar();
//Second method
call_user_func($foo->anonFunction);
//Third method that doesn't work
$foo->anonFunction();
Run Code Online (Sandbox Code Playgroud)

有没有办法在PHP中我可以使用第三种方法来调用定义为类属性的匿名函数?

谢谢

php class anonymous-function

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

在Javascript/jQuery中将匿名函数传递给自定义事件触发器

我正在尝试触发DOM元素上的自定义事件,并传递匿名函数,以便在触发事件时执行(使用jQuery).所以像这样:

$(some-dom).live("custom_event", function(evtObj, data, callback) {
//do some stuff
callback();
});

$(some-button).click(function() {
    $(some-dom).trigger("custom_event", some_data, function () {
        alert("this is my anonymous function passed as event data");
    }
});
Run Code Online (Sandbox Code Playgroud)

所以单击"some-button"应该在"some-dom"上触发"custom_event"并导致我在触发器上传递的匿名函数被执行.对?但是浏览器说自定义事件中的回调未定义.难道我做错了什么?是不允许传递匿名函数作为触发器参数?谢谢

jquery callback anonymous-function jquery-callback custom-events

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

c中的匿名代码块

这样的陈述是什么意思?

int x  = ( { int a; scanf( "%d", &a ); a ; } ) ;
Run Code Online (Sandbox Code Playgroud)

它编译并运行相当于:

int x;
scanf( "%d", &x );
Run Code Online (Sandbox Code Playgroud)

它似乎像某种匿名函数调用或其他东西,但我不确定.我没有遇到像({})以前那样的陈述,我无法在网上找到任何解释.任何帮助将非常感谢,谢谢:)

语境:

这是扩展以下代码中的宏时获得的代码:

#define SI ({int a;scanf("%d",&a);a;});
int x = SI;
Run Code Online (Sandbox Code Playgroud)

这是编程竞赛中某人使用的代码.

c macros anonymous-function parentheses

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

如何:将匿名方法转换为VB.NET

我在C#中有以下内容:

public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, double toValue, double animationDurationSeconds, EventHandler completedEvent)
{
    double fromValue = (double)animatableElement.GetValue(dependencyProperty);

    DoubleAnimation animation = new DoubleAnimation();
    animation.From = fromValue;
    animation.To = toValue;
    animation.Duration = TimeSpan.FromSeconds(animationDurationSeconds);

    //// HERE ----------------------------------------------------
    animation.Completed += delegate(object sender, EventArgs e)
    {
        //
        // When the animation has completed bake final value of the animation
        // into the property.
        //
        animatableElement.SetValue(dependencyProperty,
                                 animatableElement.GetValue(dependencyProperty));
        CancelAnimation(animatableElement, dependencyProperty);

        if (completedEvent != null)
        {
            completedEvent(sender, e);
        }
    };
Run Code Online (Sandbox Code Playgroud)

我有一些问题将匿名方法转换为VB.NET.

我的变体是

  AddHandler animation.Completed,
    Function(sender As …
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net anonymous-function c#-to-vb.net

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

在Python中使用匿名函数

我有一些代码从许多URL下载数据列表,然后调用另一个函数,传入每个结果.就像是...

def ShowUrls(self, url):
    Urls = self.Scraper.GetSubUrls(url)
    for Url in Urls:
        self.UI.addLink(
          Url[0],
          Url[1])
Run Code Online (Sandbox Code Playgroud)

这很好但运行时有很长的延迟self.Scraper.GetSubUrls,然后所有的UI调用都非常迅速.这会导致UI长时间显示"0 Urls added",然后完成.

我想要的是能够将self.UI.addlink方法传递给方法,self.Scraper.GetSubUrls以便一旦检索到每个URL就可以调用它.一旦检索到每个URL,这应该使UI显示正确的计数.

这可能吗?如果是这样,那么正确的语法是什么?

如果我在Javascript中,我会做类似......

getSubUrls(url, function(x, y) {UI.addLink(x, y)})
Run Code Online (Sandbox Code Playgroud)

然后,在getSubUrls里面做

SomeParamMethod(Pram1, Param2)
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,那么正确的语法是什么?

python anonymous-function

4
推荐指数
2
解决办法
3835
查看次数

匿名函数和引用中的php闭包&

我有:

function outside( $limit ) {

$tally = 0;

    return function() use ( $limit, &$tally ) {
        $tally++;

        if( $tally > $limit ) {
            echo "limit has been exceeded";
        }
    };
}

$inside = outside( 2 );
$inside();
$inside();
$inside();
Run Code Online (Sandbox Code Playgroud)

输出: limit has been exceeded

我的理解:

  1. on $inside = outside( 2 );将返回匿名函数并将其分配给变量$inside.匿名函数使用值$limit(2)和$tally(0).

  2. 函数$inside()被调用.此增量$tally1以某种方式记住该值,因此也是如此$limit.以前&符号的目的是什么$tally?我知道它用于创建引用,但在这种情况下它让我感到困惑.这个闭包怎么能记住它的价值$limit

任何对官方文档的引用都会有帮助!

php closures anonymous-function

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

没有参数的c#谓词

我正在尝试将谓词传递给函数,但是它没有输入。我实际上正在尝试将计算延迟到发出呼叫为止。

有没有办法使用c#谓词类型呢?如果不是为什么。

我知道使用Func可以做到这一点

Func<bool> predicate = () => someConditionBool;
Run Code Online (Sandbox Code Playgroud)

但是我想这样做:

Predicate<> predicate = () => someConditionBool;
Run Code Online (Sandbox Code Playgroud)

c# anonymous-function func

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

PHP的目的在array_map()函数中使用关键字?

我在我的应用程序中有以下代码行.任何人都可以告诉我use以下array_map()功能中关键字的用途是什么?

array_map( function($record) use ($edit_form, $otherfields, $otherfields_keys)
{
    User::parseData($record, $edit_form['metadata']);

    if (isset($otherfields[$record['user_id']])) {
        return $record + $otherfields[$record['user_id']];
    }

    return $record + $otherfields_keys;

}, $records);
Run Code Online (Sandbox Code Playgroud)

提前致谢.

php anonymous-function

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

当代码中有另一个不相关的函数时,"不是函数"错误

我有两段代码,每个代码都按预期工作:

function Test() {}

let tmp = function() {
    console.log(this)
}
tmp.call(Test)
Run Code Online (Sandbox Code Playgroud)

function Test() {}

(function() {
    console.log(this)
}).call(Test)
Run Code Online (Sandbox Code Playgroud)

它们都产生预期的输出:[Function: Test].

但是,当组合这些独立的代码片段时,会产生错误.所以,运行以下代码

function Test() {}

let tmp = function() {
    console.log(this)
}
tmp.call(Test)

(function() {
    console.log(this)
}).call(Test)
Run Code Online (Sandbox Code Playgroud)

结果是

TypeError: tmp.call(...) is not a function
Run Code Online (Sandbox Code Playgroud)

我找到了一个非优雅的解决方案,这为第二个代码片段添加了一个延迟.因此,以下将产生所需的输出([Function: Test]两次):

function Test() {}

let tmp = function() {
    console.log(this)
}
tmp.call(Test)

setTimeout(() => {
    (function() {
        console.log(this)
    }).call(Test)
}, 100);
Run Code Online (Sandbox Code Playgroud)

该超时似乎解决它这一事实使我认为这是相关的一些异步的东西,但我无法解释究竟为什么发生.

javascript this anonymous-function

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