js把字符串转换成变量

JavaScript021

js把字符串转换成变量,第1张

var str = "win"

eval("var " + str)

console.log(win)

eval(str+"=3")

console.info(win)

[javascript]

var test='{ colkey: "col", colsinfo: "NameList" }'

var test='{ colkey: "col", colsinfo: "NameList" }'

2.把文本转化为对象

[javascript]

var test='{ colkey: "col", colsinfo: "NameList" }'

var obj2=eval("("+test+")")

alert(obj2.colsinfo)

var test='{ colkey: "col", colsinfo: "NameList" }'

var obj2=eval("("+test+")")

alert(obj2.colsinfo)

3.把文本转化为数组

[javascript]

var test='["colkey", "col", "colsinfo","NameList" ]'

var obj2=eval("("+test+")")

alert(obj2.length)