I have read some material ,it says that eval is evil ,eval can be replaced wit some method.
The code snippet below can call sinajs stock price api,when you type some company's ticker in input
,its price will show in console.log's window,for example to input 600020
.
function stock(){
ticker = $("#code").val();
$.ajax({
type : "GET",
url : "http://hq.sinajs.cn/list=sh" + ticker,
dataType : "script",
cache : "false",
timeout : 2000,
success : function(data) {
sname = eval("hq_str_sh" + ticker);
console.log(sname);},
error : function() {alert("wrong");}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
tiker:
<input id="code" type="text" size="10">
<input id="Search" type="button" onclick="stock()" value="query">
How to replace the eval function with some method for the below code snippet?
success : function(data) {
sname = eval("hq_str_sh" + ticker);
console.log(sname);},
It's my try:
success : function(data) {
window["myTicker"] = "hq_str_sh" + ticker;
console.log(myTicker);},
Failed.
console.log(window["hq_str_sh" + ticker])