我想调用一个函数,其名称在变量中。
例如:
我动态获取字符串"pageTracker._trackpageview('/url/page1.page'); "并将其分配给变量,如下所示
var myFunction = pageTracker._trackpageview('/url/page1.page');";
Run Code Online (Sandbox Code Playgroud)
现在,当我提交页面时,我想执行变量 myFunction 中的函数。
谢谢你们。
我想使用 javascript 创建一个动态生成的表单,一切正常,直到我尝试将数组作为参数传递。当我这样做时,会发生错误。谁能解释一下这是什么?
这是我的代码:
var loadFrm = function(component) {
for(nItem in component) {
var myComponent = "add" + firstToUpper(component[nItem].type);
var callComponent = myComponent + "(" + component[nItem].opt + ");";
eval(callComponent);
}
}
var json = [
{
type: "scale",
opt: {content: [{label: "male", value: "m"}, {label: "female", value: "f"}]}
}
];
loadFrm(json);
Run Code Online (Sandbox Code Playgroud)
编辑这是错误:
missing ] after element list
[Break on this error] addScale([object Object]);
Run Code Online (Sandbox Code Playgroud) 我想将对象的值作为参数传递给函数。
# This is my object
anObject <- "an_unkown_string"
# I would like to do the equivalent of:
someFunc("an_unkown_string")
# .. by somehow calling on the object containing the string
someFunc( ??? (anObject) )
Run Code Online (Sandbox Code Playgroud)
例如,使用下面的示例函数(基于save()):
someFunc <- function(...) {
names <- as.character(substitute(list(...)))[-1L]
return(names)
}
# Ideally, the output would be:
someFunc( ??? (anObject) )
[1] "an_unkown_string"
Run Code Online (Sandbox Code Playgroud)
我无权修改someFunc
我已尝试以下操作,但没有成功。
someFunc(Name_of_Object)
someFunc(eval(Name_of_Object))
someFunc(evalq(Name_of_Object))
someFunc(force(Name_of_Object))
someFunc(eval(parse(text=Name_of_Object)))
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
我听说 ast.literal_eval 比 eval() 安全得多,但是在更改代码时,我收到“格式错误的字符串/节点”错误。
例如:
bar = False
incorrect = {"foo":bar}
correct = {"foo":"bar"}
ast.literal_eval(incorrect)
Run Code Online (Sandbox Code Playgroud)
返回错误但是
ast.literal_eval(correct)
Run Code Online (Sandbox Code Playgroud)
返回预期的 {"foo":"bar"}
为什么第一次评估不返回 {"foo":False}
我在解析行时遇到一些问题.实际上我在这一行中拥有一切:
'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another address\r\n\r\n'
Run Code Online (Sandbox Code Playgroud)
我希望这样这一行:
test:[abc:123];something
Name=some name;phone no.: [123456]
Address: some address; another address
Run Code Online (Sandbox Code Playgroud)
我试过了
#from ast import literal_eval
from ast import *
a = 'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another address\r\n\r\n'
msg = literal_eval(a)
# and
msg = eval(a)
Run Code Online (Sandbox Code Playgroud)
但我得到错误.有没有其他简单的方法来解决这个问题?
不明白为什么下面的代码会覆盖我的var arr.任何帮助赞赏.
var arr = [1,2,3]
var string = "function swap(arr) { var newarr = arr; var temp = arr[0]; newarr[0] = arr[arr.length-1]; newarr[arr.length-1] = temp; return newarr }"
var test = eval("[" + string + "]")[0];
test(arr);
console.log(arr);
//this outputs [3,2,1]
test(arr);
console.log(arr);
//this outputs [1,2,3]
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试将列表的以下字符串转换回列表。
[('zX7XjZ1Vwai5UbqNDDJ1NQ', 570512, [155])]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了这两种方法eval(),ast.literal_eval但是由于某种原因,当我打印转换后的字符串的类型时,它仍然是一个字符串(但删除了引号)
我也试过使用 json.loads()
似乎无论我多么努力,我都无法将这个列表字符串转换为 Python 列表!
(def a (edn/read-string "(+ 1 3)"))
; => (+ 1 3)
Run Code Online (Sandbox Code Playgroud)
如何评估此结果列表?
(type (first a))
; => cljs.core/Symbol
(= (first a) '+)
; => true
Run Code Online (Sandbox Code Playgroud)
我想更一般地我将如何从符号->函数中得到。这是Clojure的正常做法吗?我似乎找不到任何东西。也许我没有搜索正确的术语。
我有一个作业,有一个问题我就是不明白。他们要求的是什么?不确定这是否是一个非常合适的问题,但我完全处于大脑冻结状态,所以如果有人提供帮助,那将意味着很多!
题:
让我们考虑用户定义的数据类型 data Ast = V Int | P AST | M Ast Ast 我们假设每个叶子 V x 代表数字 x,而 P 和 M 代表它们参数的加法和乘法。编写函数
eval :: Ast -> Int
Run Code Online (Sandbox Code Playgroud)
它将这样的 Ast 评估为算术表达式,例如
eval (V 5) = 5
eval (P (V 55) (M (V 2) (V 3))) = 55 + (2 * 3) = 61
eval (M (P (V 12) (V 3)) (M (V 2) (V 5))) = (12 + 3) * (2 * 5) = 150
Run Code Online (Sandbox Code Playgroud) 我想使用字典本身来评估字典键的值。例如:
dict_ = {'x': 1, 'y': 2, 'z':'x+y'}
dict_['z'] = eval(dict_['z'], dict_)
print(dict_)
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它在字典中包含了一堆不必要的东西。在上面的例子中,它打印:
{'x': 1, 'y': 2, 'z': 3, '__builtins__': bunch-of-unnecessary-stuff-too-long-to-include
Run Code Online (Sandbox Code Playgroud)
Instead, in the above example I just want:
{'x': 1, 'y': 2, 'z': 3}
Run Code Online (Sandbox Code Playgroud)
How to resolve this issue? Thank you!
eval ×10
python ×4
javascript ×3
dictionary ×2
clojure ×1
edn ×1
evaluation ×1
form-submit ×1
haskell ×1
json ×1
list ×1
object ×1
parsing ×1
r ×1
string ×1