了解JavaScript中的范围

时间:2017-07-24 21:10:51

标签: javascript

为什么下面的printActions函数中的'this'关键字可以访问'actions'数组,但forEach循环内的匿名函数中的'this'关键字无法访问'first'name属性?< / p>

var person = {
    first: "Steve",
    actions: ['run', 'swim', 'bike'],
    printActions: function () {
         this.actions.forEach(function (action) {
            var str = this.first + " likes to " + action;
            console.log(str);

        }); //end forEach
    }//end printActions function

};// person object
person.printActions();

0 个答案:

没有答案