在函数内部定义的函数 this 指向 undefined

在函数内部定义的函数 this 指向 undefined

以下这个 this 就是指向 undefined.

但在非 strict 模式下是指向 window

    <script>
    'use strict';

    var xiaoming = {
        name: '小明',
        birth: 1990,
        age: function () {
            function getAgeFromBirth() {
                var y = new Date().getFullYear();
                return y - this.birth;
            }
            return getAgeFromBirth();
        }
    }
    console.log(xiaoming.age());
    </script>

这是一个设计错误,但已经没有改变,不过还好有补救办法。

posted on 2019-01-05 13:37  建伟F4nniu  阅读(582)  评论(0编辑  收藏  举报

导航