#ruby 中的闭包
# Creates a new <code>Proc</code> object, bound to the current
# context. <code>Proc::new</code> may be called without a block only
# within a method with an attached block, in which case that block is
# converted to the <code>Proc</code> object.
#
sum = 0
10.times{|n| sum += n}
print sum
def upto(from,to)
while from <= to
yield from
from+=1
end
end
upto(1,10) {|n| puts n}
def counter()
i = 1
Proc.new{ puts i;i+=1}
end
c = counter()
c.call() 1
c.call() 2
/*javascript中的闭包*/
function f1(){
n=999;
function f2(){
alert(n);
}
return f2;
}
var result=f1();
result(); // 999
//用途 setInterval 传参数
function do_load_stock(market,code)
{
return function(){load_stock(market,code)};
}
function time_loader(market,code)
{
var stock = market+code;
if(CheckStockTime(stock))
{
setInterval(do_load_stock(market,code),30000);
}
}