📜  javascript this = that - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:27.850000             🧑  作者: Mango

代码示例1
var colours = ['red', 'green', 'blue'];
document.getElementById('element').addEventListener('click', function() {
    // this is a reference to the element clicked on

    var that = this;

    colours.forEach(function() {
        // this is undefined
        // that is a reference to the element clicked on
    });
});