箭头函数
注意:发送网络请求时,回调函数必须要使用箭头函数。
<script>
function f1(name,age){
console.log(name,age);
}
f1("失败",99);
//箭头函数
let f2 = (name,age) =>{
console.log(name,age);
}
f2("订单",99);
</script>
<script>
var name = "啊vv";
let info = {
name: "szw'",
func: function () {
console.log(this.name); // 函数内部默认都有this关键字
}
}
info.func();
function getData() {
console.log(this.name); // 函数内部默认都有this关键字,this=window
}
getData();
</script>
<script>
var name = "是vv";
let info = {
name: "szw",
func: function () {
console.log(this.name); // 函数内部默认都有this关键字,this=info对象
function getData() {
console.log(this.name); // 函数内部默认都有this关键字,this=window
}
getData();
}
}
info.func();
</script>
<script>
var name = "svv";
let info = {
name: "szw",
func: function () {
console.log(this.name); // 函数内部默认都有this关键字,this=info对象
let getData = () => {
console.log(this.name); // 函数内部默认都有this关键字
}
getData();
}
}
info.func();
</script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
let info = {
data: null,
send: function () {
axios({
method: "post",
url: "https://api.luffycity.com/api/v1/auth/password/login/?loginWay=password",
data: {
username: "szw",
password: "sss"
},
headers: {
"content-type": "application/json"
}
}).then(function (res) {
console.log(this,res);
this.data = res;//data没改
})
}
}
info.send();
</script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
let info = {
data: null,
send: function () {
axios({
method: "post",
url: "https://api.luffycity.com/api/v1/auth/password/login/?loginWay=password",
data: {
username: "alex",
password: "dsb"
},
headers: {
"content-type": "application/json"
}
}).then((res) => {
console.log(this,res);
this.data = res; //data改了
})
}
}
info.send();
</script>
本文作者:Sherwin
本文链接:https://www.cnblogs.com/sherwin1995/p/16891712.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步