eval ,proc

On Behalf Of Damjan Rems:
# a1=10
# r= ?('a' + '1')  # ? is whatever, r should have value 10

<script type="text/javascript"></script> some simple ways,

A. using eval
>> a1=10
=> 10
>> macro="a1+1"
=> "a1+1"
>> eval(macro)
=> 11

B. using proc or lambda
>> macro2 = proc{a1+1}
=> #<Proc:0xb7dd6bcc@(irb):7>
>> a1=100
=> 100
>> macro2.call
=> 101

有时光记得eval却忘记了proc

你可能感兴趣的:(JavaScript)