我需要在Rails 4应用程序中执行javascript函数。我在ExecJS (https://github.com/sstephenson/execjs)中尝试了以下操作,但没有成功:
my_js_function = 'function my_function(a){var b=a.split("$");var c="";for(i=0;i<=b.length-1;i++){if(!(i%2)){c+=b[i]}}var b=c.split("");c="";for(i=0;i<=b.length-1;i++){if(i%2){c+=b[i]}}return c}'
compiled_function = ExecJS.compile(my_js_function)
result = compiled_function.call(my_argument)最初的JS函数接受一个参数(my_argument),我需要将它传递给它。任何帮助,如何这是可能的,将是非常感谢!
发布于 2014-01-16 11:01:30
你们关系很好。您不应该使用call,您应该以这种方式使用eval:
my_js_function = 'function my_function(a){var b=a.split("$");var c="";for(i=0;i<=b.length-1;i++){if(!(i%2)){c+=b[i]}}var b=c.split("");c="";for(i=0;i<=b.length-1;i++){if(i%2){c+=b[i]}}return c}'
compiled_function = ExecJS.compile(my_js_function)
compiled_function.eval('my_function("heyhey")')
#=> ehyhttps://stackoverflow.com/questions/21109797
复制相似问题