当点击每个div时,如果单击div 1,它应该警告'1'或如果点击div 2则警告'5'.我试图尽可能地使这些代码变得容易,因为在更大的应用程序中需要这样做.
<html>
<head>
<style type="text/css">
#div1 { background-color: #00ff00; margin: 10px; padding: 10px; }
#div2 { background-color: #0000ff; margin: 10px; padding: 10px; }
</style>
<script type="text/javascript">
function init()
{
var total = 1;
var div1 = document.getElementById('div1'),
div2 = document.getElementById('div2');
var helper = function(event, id)
{
if (event.stopPropagation) event.stopPropagation();
if (event.preventDefault) event.preventDefault();
alert('id='+id);
}
div1.addEventListener('click', function(event) { helper(event, total); }, false);
total += 4;
div2.addEventListener('click', function(event) { helper(event, total); }, false);
}
</script>
</head>
<body onload="init();">
<div id="div1">1</div> …Run Code Online (Sandbox Code Playgroud) 我正在研究一种需要在不同位置重复一个小操作的方法,但要重复的代码应该是该方法的私有代码.显而易见的解决方案是嵌套函数.无论我尝试什么,C#编译器对我来说都是barfs.
大致等于这个Perl片段的东西:
my $method = sub {
$helper_func = sub { code to encapsulate };
# more code
&$helper( called whenever needed );
# more code
}
Run Code Online (Sandbox Code Playgroud)
正是我在谈论的,以及我在C#中想要实现的目标.
类中没有其他方法应该能够在此上下文中访问辅助函数.在C#中编写这个构造的最合理的方法,就像在我看来会是这样的:
var helper = (/* parameter names */) => { /* code to encapsulate */ };
Run Code Online (Sandbox Code Playgroud)
实际上让编译器获得了保持.
由于这样的赋值是被禁止的,因为使用较旧的delegate(){}语法代替lambda,因此在方法中声明一个委托类型 - 然而csc实际上允许我写的是,这是:
private delegate /* return type */ Helper(/* parameters */);
private /* return type */ method(/* parameters */) {
Helper helper = (/* parameter names */) => {
/* code to …Run Code Online (Sandbox Code Playgroud) 我有一个功能,
public SharpQuery Each(Action<int, HtmlNode> function)
{
for (int i = 0; i < _context.Count; ++i)
function(i, _context[i]);
return this;
}
Run Code Online (Sandbox Code Playgroud)
其中为上下文的每个元素调用传入函数.是否可以设置"this"指的是什么内部Action<int, HtmlNode> function?
例如,
sharpQuery.Each((i, node) => /* `this` refers to an HtmlNode here */);
Run Code Online (Sandbox Code Playgroud) 如何在Objective-C中分配并随后调用一个将函数返回给局部变量的函数?
更新:
我想出了以下内容,但它仍然不对我害怕:
(void (^)()) (^loadedCallback) () = (void (^)()) ^(){
@synchronized (synchronizer) {
semaphore++;
}
return Block_copy(^{
@synchronized (synchronizer) {
semaphore--;
if (semaphore == 0) {
onAllLoaded();
}
}
}); };
Run Code Online (Sandbox Code Playgroud) lambda function objective-c anonymous-function objective-c-blocks
如何在此示例中从函数中获取变量名称:
// it should return A
var A = function(){ console.log(this.name); }
Run Code Online (Sandbox Code Playgroud)
有这样的事吗?
我想创建一个客户端函数,可以使用客户端变量接收和执行任意命令.我将从我的服务器发送这些函数,使用socket.io发送一个包含匿名函数的JSON对象,这将是我的命令.它看起来像下面这样:
//client side
socket.on('executecommand', function(data){
var a = "foo";
data.execute(a); //should produce "foo"
});
//server side
socket.emit('executecommand', {'execute': function(param){
console.log(param);
}});
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试它时,客户端收到一个空的json对象(data == {}),然后抛出异常,因为数据不包含方法执行.这里出了什么问题?
如何确定闭包声明的参数个数以便在闭包之外使用?例如:
$myClosure = function($arg1, $arg2, $arg3){
}
$numArgs = someMagicalFunction($myClosure);
echo("that closure expects $numArgs arguments");
Run Code Online (Sandbox Code Playgroud)
是否有一些功能可以满足我的需求?
我尝试在Matlab脚本中编写递归内联匿名函数.
这是一个MWE:
funR = @(x) [x(1) funR(x(2:end))];
funR(0:5);
Run Code Online (Sandbox Code Playgroud)
但是这引发了以下异常:
未定义的函数或变量'funR'.
这在函数文件中运行时有效,但在脚本中运行时则无效.这是因为Matlab确实以不同方式阅读了这些内容
我对这个MWE的预期结果是:
[0, 1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
怎么做对了?
目标是将funR定义为内联函数,因此两个或更多行解决方案不是我想要的.如果这个或MWE有任何意义,请忽略,这不是这个问题的重点.
我知道这对于非菜鸟 C++开发者来说似乎非常愚蠢,但这4个lambda表达式之间有什么区别?
代码:
#include <iostream>
#include <math.h>
#include <functional>
inline double MyFunction(double a, double b, double c) {
return (a + b + c);
}
inline void FunctionWrapper(std::function<double(double)> tempFunct, double value) {
std::function<double(double)> funct;
funct = tempFunct;
std::cout << "result: " << funct(value) << std::endl;
}
int main()
{
double value = 100.0;
FunctionWrapper([](double value) { return MyFunction(value, 1.0, 2.0); }, value);
FunctionWrapper([](double value) -> double { return MyFunction(value, 1.0, 2.0); }, value);
FunctionWrapper([value](double value) { return …Run Code Online (Sandbox Code Playgroud) 我有一个一般性的问题:在下面的C#代码中tFour无法创建线程,并且编译器向我显示以下错误:“将匿名函数转换为void返回的委托无法返回值 ”
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DelegatesAndLambda
{
class Program
{
public static int ThrowNum(object a)
{
Console.WriteLine(a);
return 2 * (int)a;
}
static void Main(string[] args)
{
Func<int> newF = delegate () { int x = ThrowNum(2); return x; };
Thread tOne = new Thread( delegate () { int x = ThrowNum(2); });
Thread tTwo= new Thread(()=> ThrowNum(2));
Thread tThree = new Thread(() => newF());
Thread …Run Code Online (Sandbox Code Playgroud)