我正在浏览JIT的代码,我看到了这个:
var isGraph = ($type(json) == 'array');
var ans = new Graph(this.graphOptions);
if(!isGraph)
//make tree
(function (ans, json) {
ans.addNode(json);
for(var i=0, ch = json.children; i<ch.length; i++) {
ans.addAdjacence(json, ch[i]);
arguments.callee(ans, ch[i]);
}
})(ans, json);
else
//make graph
(function (ans, json) {
var getNode = function(id) {
for(var w=0; w<json.length; w++) {
if(json[w].id == id) {
return json[w];
}
}
return undefined;
};
Run Code Online (Sandbox Code Playgroud)
那些匿名函数的目的是什么?他们立即超出范围,对吧?
为何使用:
(function (ans, json) {
ans.addNode(json);
for(var i=0, ch = json.children; i<ch.length; i++) {
ans.addAdjacence(json, ch[i]); …Run Code Online (Sandbox Code Playgroud) 我的目标是在if()语句中放入一些复杂的逻辑.假设我有一个值数组,如果我的数组中的所有内容都非零,我将执行一些代码.通常情况下,我可以说,$valid = true,foreach我的阵列,并设置$valid = false当发现一个零.然后我会运行我的代码if ($valid).或者,我可以将我的循环放入一个函数中并将函数放在我的函数中if().
但是我很懒,所以我宁愿不要使用一堆"有效"标志,我宁愿不写一个只在一个地方使用的全新功能.
所以让我说我有这个:
if ($q = function() { return 'foo'; }) {
echo $q;
}
else {
echo 'false';
}
Run Code Online (Sandbox Code Playgroud)
我期待if得到的'foo',得分为真.我的闭包致力于$q并且语句执行.$q返回字符串foo,并打印'foo'.
相反,我得到了错误Object of class Closure could not be converted to string.
所以让我们试试这个:
if (function() { return false; }) {
echo 'foo';
}
else {
echo 'true';
}
Run Code Online (Sandbox Code Playgroud)
我期待我的函数返回false并打印'true'.相反,打印'foo'.
我正在采取的方式有什么问题?好像它在说,"是的,这肯定是一个功能!" 而不是"不,因为函数评估为假".有没有办法做我想做的事情?
在John Resig的书"Pro Javascript技术"中,他描述了一种使用以下代码生成动态对象方法的方法:
// Create a new user object that accepts an object of properties
function User(properties) {
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
for (var i in properties) {
(function() {
// Create a new getter for the property
this["get" + i] = function() {
return properties[i];
};
// Create a new setter for the property
this["set" + i] = function(val) {
properties[i] = val;
}; …Run Code Online (Sandbox Code Playgroud) 我在Javascript中看到过这样的自调用函数:
(function () {
// foo!
})();
Run Code Online (Sandbox Code Playgroud)
但我也看到他们这样写:
(function () {
// bar!
}());
Run Code Online (Sandbox Code Playgroud)
从语法上讲,它们完全相同.实际上,我的个人习惯是第一种格式,但是我应该注意两者之间有什么区别吗?像浏览器纠结或其他什么?
例如,一个非常微不足道的事情是,如果第二种格式应该可靠地工作,那么这意味着这样的事情也应该是可能的:
function () {
// shut the front door! saved two characters right there!
}();
Run Code Online (Sandbox Code Playgroud)
尽管如此,这会伤害可读性.
1:
Func<int, int> myFunc = new Func<int,int>(delegate(int x) {
return x + 1;
});
Run Code Online (Sandbox Code Playgroud)
2:
Func<int, int> myFunc = delegate(int x) {
return x + 1;
};
Run Code Online (Sandbox Code Playgroud)
3:
Func<int, int> myFunc = x => x + 1;
Run Code Online (Sandbox Code Playgroud)
他们之间有什么区别?
我希望能够在pascal中动态生成弹出菜单.
我还希望能够为每个菜单项动态分配OnClick处理程序.
这是我习惯于在C#中做的事情,这是我在pascal中的尝试.
菜单项onClick事件处理程序需要属于一个对象(of Object),所以我为此创建一个容器对象.
这是我的代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus;
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TFoo = class
public
Bar : String;
Val : Integer;
end;
TNotifyEventWrapper = class
private
FProc: TProc<TObject>;
I : Integer;
public
constructor Create(Proc: TProc<TObject>);
published
procedure Event(Sender: TObject);
end;
var
Form1: TForm1;
NE : TNotifyEventWrapper;
implementation
{$R …Run Code Online (Sandbox Code Playgroud) 我正在读一本名叫"John Ninja的秘密"的好书,由John Resig和Bear Bibeaoult撰写.在第3.2章中,它给出了一个例子;
var canFly = function(){ return true; };
Run Code Online (Sandbox Code Playgroud)
然后它说;
创建匿名函数并将其分配给名为canFly的全局变量.由于JavaScript的功能特性,可以通过此引用调用该函数作为canFly().在这方面,它几乎在功能上等同于声明一个名为"canFly"的命名函数,但并不完全相同.一个主要区别是函数的name属性是"",而不是"canFly".
但是当我尝试在Chrome的开发者工具上执行该示例并检查函数的name属性时canFly,它返回值"canFly"而不是空字符串.
canFly.name;
// > "canFly"
Run Code Online (Sandbox Code Playgroud)
分配给变量的匿名函数在前几天没有名称吗?如果是这样,发生了什么变化?或者作者犯了错误?
我首先问过这个关于final在Java 中使用匿名内部类的问题:
为什么我们使用带有匿名内部类的final关键字?
我实际上正在阅读马丁奥德斯基的Scala书.似乎Scala简化了很多Java代码,但对于Scala闭包,我注意到了一个显着的差异.
在Java中,我们使用匿名内部类"模拟"闭包,捕获最终变量(将被复制到堆上而不是堆栈中),在Scala中我们可以创建一个可以捕获val的闭包,但是也是一个var,因此在闭包调用中更新它!
所以我们可以使用没有final关键字的Java匿名内部类!我还没读完这本书,但是现在我没有找到关于这种语言设计选择的足够信息.
谁能告诉我,为什么马丁Odersky的,谁似乎真的照顾功能的副作用,选择关闭,以便能够同时捕捉val和var,而不是唯一的val?
Java和Scala实现的优点和缺点是什么?
谢谢
遇到一些在表达式中使用IIFE而不仅仅是普通函数的代码.
var custom_type = (function() {
return $('#myDiv').attr('custom_type');
})();
Run Code Online (Sandbox Code Playgroud)
通常我会写这样的东西:
var custom_type = function() {
return $('#myDiv').attr('custom_type');
};
Run Code Online (Sandbox Code Playgroud)
IIFE的原因是什么?我唯一能想到的是,IIFE可能custom_type只在开始时分配变量一次,而第二次可能会在每次引用变量时继续检查更新的类型.
我们知道,可以在R中调用函数而不将它们分配给环境,例如
> (function(x){x/2})(5)
[1] 2.5
Run Code Online (Sandbox Code Playgroud)
我想在mutate_each(或summarise_each)调用中动态使用这些函数.例如,用
df <- data.frame(a = runif(10), b = rnorm(10))
我可能会尝试执行以下操作之一,但它们都会返回错误:
library(dplyr)
> df %>%
+ mutate_each(funs((function(x){x/2})), a, b)
Error in eval(substitute(expr), envir, enclos) :
Unsupported type CLOSXP for column "a"
>
> df %>%
+ mutate_each(list((function(x){x/2})), a, b)
Error: is.fun_list(calls) is not TRUE
>
>
> df %>%
+ mutate_each(funs((function(x){x/2})(.)), a, b)
Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) :
values must be length 1,
but FUN(X[[1]]) result is length 2
> …Run Code Online (Sandbox Code Playgroud)