箭头函数

注意:发送网络请求时,回调函数必须要使用箭头函数。

<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>
posted @ 2022-11-15 11:00  Sherwin_szw  阅读(2)  评论(0编辑  收藏  举报