<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>測試</title> <script> "use strict"; var user = {age:15,name:"web",getAge:function(){ alert(this.age) }}; var jorden = { age:30,name:99 } var text = '{ "sites" : [' + '{ "name":"Runoob" , "url":"www.runoob.com" },' + '{ "name":"Google" , "url":"www.google.com" },' + '{ "name":"Taobao" , "url":"www.taobao.com" } ]}'; function checkForm(){ //alert(this);//"use strict"; 嚴格模式下 返回undefined ;非嚴格模式返回window 就是該全局對象爲 user.getAge(); //輸出:15 var age = user.getAge.call(jorden); //返回age=30; user.getAge();//輸出:15 //字符串轉json解析 var obj = JSON.parse(text); console.log(obj['sites'][0]['name']); //返回sites對象的第一元素Runoob //json對象轉換成字符串 var str = {"name":"菜鳥教程", "site":"http://www.runoob.com"}; var str_pretty1 = JSON.stringify(str); console.log(typeof str +" "+ typeof str_pretty1); //輸出object string 一個是json對象;一個是字符串 } </script> </head> <body> <form action="#" method="get" name="myform"> <input name="username" value="" type="text"> <input type="submit" value="提交" onclick="checkForm()" > <input type="reset" value="重置"> </form> </body> </html>