JavaScript对象this指向(普通键this指向 非指向函数的键)

1、结论

JavaScript对象普通键(非指向函数的键)this指向是window。

2、示例

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>JavaScript对象this指向(普通键this指向 非指向函数的键)</title>
    </head>

    <body>
        <script type="text/javascript">
            var a=1;
            var b = {
                a: 2,
                c: this.a + 10,
                d: eval(console.log(this)),
                getA: function() {
                    console.log(this.a);
                }
            };
            //setTimeout的this指向window,相当于window.setTimeout
            setTimeout(b.getA, 10); //1
            //this指向window
            console.log(b.c); //11 
            //this指向b对象
            b.getA(); //2
        </script>
    </body>

</html>

输出:

原文地址:https://www.cnblogs.com/mengfangui/p/8670416.html