首页 > Web开发 > 详细

call JSON.parse JSON.stringify typeof 的使用及严格模式this的使用

时间:2019-03-29 12:35:11      阅读:139      评论:0      收藏:0      [点我收藏+]
<!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> 

  

call JSON.parse JSON.stringify typeof 的使用及严格模式this的使用

原文:https://www.cnblogs.com/wanglijun/p/10620055.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!