<body>
<script>
//对象初始化(传统方法)
var student={
name:"Mike",
age:20,
go:function(){
console.log("我骑着自行车去上课");
}
};
//console.log(student);
//对象初始化(ES6语法)
//因为对象是key:value存在
//1.如果key和变量的名字一致,可以只定义一次即可
//2.如果value是一个函数,可以把:function()取得只留下()
var name="Mike";
var age=20;
let student2={
name,
age,
go(){
console.log("我骑着自行车去上课");
}
}
student2.go();
console.log(student2);
</script>
</body>
应用场景:
<body>
<form>
账号:<input type="text" id="account" />
<br>
密码:<input type="text" id="password" />
<br>
<input type="button" value="登录" id="logbtn" />
</form>
<script>
$("#logbtn").on("click",function(){
var account=$("#account").val();
var password=$("#password").val();
//对象简写的应用场景,下面用的是ES6语法
var params={account,password};
//执行异步请求
$.ajax({
type:"post",
url:"xxx",
data:params,
success(){
}
})
})
</script>
</body>