<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="./vue.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 字符串 -->
<p>{{ str }}</p> <!--页面展示:字符串-->
<p>{{ num + 'aaa'}}</p><!--页面展示:1aaa-->
<p>{{ str.length }}</p> <!--页面展示:3-->
<!-- 数值 -->
<p>{{ num }}</p> <!--页面展示:1-->
<p>{{ num+num1 }}</p> <!--页面展示:101-->
<p>{{ num > num1 }}</p> <!--页面展示:false-->
<p>{{ num.toFixed(2) }}</p> <!--页面展示:1.00-->
<!-- boolean -->
<p>{{ flag }}</p> <!--页面展示:true-->
<!-- 数组 -->
<p>{{ arr }}</p> <!--页面展示:[1,2,3,4]-->
<p>{{ arr[3] }}</p> <!--页面展示:4-->
<!-- 对象 -->
<p>{{ obj }}</p> <!--页面展示:{ "name": "tom", "age": 20 }-->
<p>{{ obj.name }}</p> <!--页面展示:tom-->
<!-- null/undefined/NaN -->
<p>{{ arg1 }}</p> <!--页面展示:-->
<p>{{ arg2 }}</p> <!--页面展示:-->
<p>{{ arg3 }}</p> <!--页面展示:NaN-->
<!-- 三目运算符 -->
<p>{{ num > num1 ? "是" : "否" }}</p> <!--页面展示:否-->
</div>
<script>
new Vue({
el:"#app",
data:{
str: '字符串',
num: 1,
num1:100,
flag: true,
arr: [1,2,3,4],
obj:{
name:'tom',
age:20
},
arg1: null,
arg2: undefined,
arg3: NaN
}
})
</script>
</body>
</html>
// undefined == null; 比较如果是同类型则直接对比,如果不是则转换成Boolean进行对比
// typeof数组 也是object
// 对象如果使用toString(),则转换成[object object]
function toString (val) {
return val == null? ''//如果是null,则转换成空字符串
: typeof val === 'object'//如果是对象,使用JSON.stringify转换成字符串
? JSON.stringify(val, null, 2)
: String(val)//如果是两者都不是,则强转成字符串
}