我有一个MapHandler类.
我创建了一个对象myMaphandler = new MapHandler并调用了initialize方法.但@ userLocationMarker.getPosition()返回null :(
如果我将评论提醒并从Chrome JS控制台调用@ userLocationMarker.getPosition(),我将获得必要的坐标.
class window.MapHandler
initialize: (centerLocation) ->
@makeMap(centerLocation)
@defineUserLocation()
alert @userLocationMarker.getPosition()
makeMap: (centerLocation) ->
myOptions =
zoom: 14
center: centerLocation
mapTypeId: google.maps.MapTypeId.ROADMAP
@map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
placeMarker: (location, icon_path) ->
if icon_path
markerImage = new google.maps.MarkerImage(icon_path, null, null, null, new google.maps.Size(25, 25))
else
markerImage = null
marker = new google.maps.Marker(
position: location
map: @map
icon: markerImage)
defineUserLocation: () ->
@userLocationMarker = @placeMarker(null, null)
handleMap = (position) =>
pos = new google.maps.LatLng(position.coords.latitude, …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行以下PHP代码:
$path_hierarchy = // function that returns an array
return array_reduce(
$terms,
function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
}
);
Run Code Online (Sandbox Code Playgroud)
...但是我收到以下PHP错误:
PHP Parse error: syntax error, unexpected ')', expecting '{'
所以,我切换到以下语法:
$path_hierarchy = // function that returns an array
$callback = function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
};
return array_reduce(
$terms,
$callback
);
Run Code Online (Sandbox Code Playgroud)
......这很有效.我不能use在匿名函数的上下文中使用关键字作为另一个函数的参数吗?
我bar()在另一个函数中嵌套了一个函数定义foo().现在我试图foo()从嵌套函数访问位于外部函数中的变量bar().但是,由于范围规则,这不起作用(请参阅下面的错误追溯).
我正在寻找类似于global关键字的东西,但这只能让我访问全局变量,而这是某种半全局变量.
这是示例代码:
def foo():
i = 0
def bar():
# how can I get access to the variable from enclosing scope?
i += 1
bar()
foo()
Run Code Online (Sandbox Code Playgroud)
输出是:
$ python test.py
Traceback (most recent call last):
File "test.py", line 7, in <module>
foo()
File "test.py", line 5, in foo
bar()
File "test.py", line 4, in bar
i += 1
UnboundLocalError: local variable 'i' referenced before assignment
Run Code Online (Sandbox Code Playgroud) 将函数作为参数传递给类构造函数时如何避免闭包?
我的类构造函数接受一个类型的函数() => Unit.在程序工作过程中,我想从一个可变映射中访问一个键值对,这个映射在使用构造函数创建对象时没有定义
val cats = scala.collection.mutable.Map[String, Cat]
class Trigger(period: Long, f: () => Unit) {
//pseudocode:
when period passes, f()
}
someWonderfulObject += new Trigger (1000, () => cats("Hershy").meow)
cats += ("Hershy" -> Cat())
Run Code Online (Sandbox Code Playgroud)
然后,当奇妙的物体触发它的触发器时,我得到的错误是没有像"Hershy"那样的键.我的结论是,显然,这是由于在没有"Hershy"的状态下f传递给Triggercons被关闭.
我现在的问题是 - 如何避免Scala在这里使用闭包而是看看实际的状态cats?
更新:
"当期间通过"的代码是这样的:
def update(tpf: Float) {
timePassed += tpf
if(timePassed > period) f()
}
Run Code Online (Sandbox Code Playgroud)
该tpf值来自上面,所以在这里这一切似乎确定.
更新:
我发现问题出在另一个完全无关的地方,我把所有这些东西的容器弄糊涂了.然而,谢谢大家,我从答案中学到了一些东西.
我听说C没有闭包,今天我在Objective-C中看到了闭包的使用.Objective-C是否支持闭包而不是C?
更新:感谢所有答案. 我也在网上找到了这个指南:http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1
我想计算IEnumerable列表中元素的等级并将其分配给成员.但是下面的代码仅在第一次调用时起作用.第二次通话从最后一个等级值开始.所以我没有输出012和012,而是输入了012和345
class MyClass
{
public string Name { get; set; }
public int Rank { get; set; }
}
public void SecondTimeRankEvaluvate()
{
MyClass[] myArray = new MyClass[]
{
new MyClass() { Name = "Foo" },
new MyClass() { Name = "Bar" },
new MyClass() { Name = "Baz" }
};
int r = 0;
var first = myArray.Select(s => { s.Rank = r++; return s; });
foreach (var item in first)
{
Console.Write(item.Rank);
}
// Prints 012
Console.WriteLine("");
foreach …Run Code Online (Sandbox Code Playgroud) 是否有更简单和/或更易读的方法在Ruby中创建闭包,以便定义的方法可以访问变量 m?
我在lambda这里有一个轻微的"问题" .
我经常动态定义必须访问局部变量的方法:
例如:
class Comparison
def income
123
end
def sales
42342
end
# and a dozen of other methods
# Generate xxx_after_tax for each method
instance_methods(false).each do |m|
lambda {
define_method("#{m}_after_tax") do
send(m) * 0.9
end
}.call
end
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一段非常简单的代码,但无法找到一个优雅的解决方案:
int count = 0;
jdbcTemplate.query(readQuery, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
realProcessRow(rs);
count++;
}
});
Run Code Online (Sandbox Code Playgroud)
这显然不能编译.我知道的两个解决方案都很臭:我不想计算一个类字段,因为它实际上是一个我需要用于记录目的的局部变量.我不想数数数组,因为它很难看.
这只是愚蠢的,必须有一个合理的方法来做到这一点?
试图找出为什么php匿名函数只有在函数头中给出参数时才有效.
例如
$f = function(){
echo "hello world";
};
$f;
$f();
Run Code Online (Sandbox Code Playgroud)
不行.但
$f = function($argument){
echo $argument;
}
$f('hello world');
Run Code Online (Sandbox Code Playgroud)
工作得很好.
为什么它需要参数,是否有任何解决方法?
编辑
这必须是版本问题.我在5.3.18,我的第一个例子肯定不起作用.对于那些不相信的人,它会抛出:
Parse error: syntax error, unexpected T_FUNCTION in index.php(192) :
eval()'d code on line 1
Run Code Online (Sandbox Code Playgroud)
编辑
在看了DaveRandom的回答后,我又回到了不知道发生了什么.那就是如果它们是正确的,它在5.3.10中有效......
我有这样的代码:(并在jsfiddle http://jsfiddle.net/k6zNm/3/)
(function(){
Marker = function(opts){
var marker = this;
marker.Version = "2012.Jul.06";
marker.HelloWorld = function(){
return marker.Version;
}
}
})();
window.mymarker = new Marker();
$("div#message").text(mymarker.HelloWorld());
Run Code Online (Sandbox Code Playgroud)
代码工作正常.但我认为这(function(){})();是一个封闭.为什么我可以访问Marker它.这不是对全球命名空间的污染吗?
closures ×10
javascript ×2
php ×2
c ×1
c# ×1
coding-style ×1
coffeescript ×1
java ×1
objective-c ×1
python ×1
reduce ×1
ruby ×1
scala ×1
scope ×1
variables ×1