开发者

passing blocks around in javascript without lengthy function syntax

开发者 https://www.devze.com 2023-03-20 23:17 出处:网络
In my testing code, I\'m getting a lot of things that look like this: test.truth(function(){return myTest.isDef(\'\')});

In my testing code, I'm getting a lot of things that look like this:

test.truth(function(){return myTest.isDef('')});

Where a majority of t开发者_Python百科he line there is just the function boilerplate. Is there a way to reduce that, so that I can have a syntax more like:

test.truth { myTest.isDef('') }

as one would do in Scala?

Of course, ideally this is highly browser compatible.


If you're looking for concise syntax, you may want to give CoffeeScript a look.


No, sorry. Javascript has array literals - [] and object literals - {}, but function literals are only possible using function() { } syntax


You could use eval

function testtrutheval(someobject, evaltext)
{
   test.truth(function {  eval("someobject." + evaltext);});
}

And then write

Testtrutheval(mytest,"isdef('')");

I hope i got the syntax right, its been 8 years sinc i did javascript.

0

精彩评论

暂无评论...
验证码 换一张
取 消