แปลงสตริงเป็น function ใช้เมธอด eval() รับ string เป็นพารามิเตอร์ แปลงเป็นฟังก์ชัน
รูปแบบ
eval(string);
ตัวอย่าง
ในสตริง property 'age' กำหนดเป็นฟังก์ชัน ใช้ eval() แปลง property age เป็นฟังก์ชัน แสดงผล
<html> <body> <script>
var string = '{"name":"Ram", "age":"function() {return 27;}", "city":"New jersey"}';
var fun = JSON.parse(string);
fun.age = eval("(" + fun.age + ")");
document.write(fun.name + " " + "of Age" + " " + fun.age() + " " + "from city" + " " + fun.city);
</script> </body> </html>
ผลลัพธ์
Ram of Age 27 from city New jersey