自定义对象
案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var user = {
name: "张三",
age: 18,
food: function(){
return 'meat';
},
sport(){
return 'football';
}
}
console.log(user.name);
console.log(user.food());
console.log(user.sport());
</script>
</body>
</html>
JSON
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var jsonstr = '{"name": "zs" ,"age": 18 ,"addr" = ["北京","福建"]}';
console.log(jsonstr);
/* JSON字符串转为JS对象 */
var jsobj = JSON.parse(jsonstr);
console.log(jsobj.age)
/* JS对象转为JSON字符串 */
var j = JSON.stringify(jsobj);
console.log(j);
</script>
</body>
</html>
运行结果不知道为什么报错,也没找到原因,有大佬知道吗