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

时间:2022-06-30 07:20:26

在函数内部定义的函数 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>

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

相关文章