var中的这个=在javascript中

时间:2017-04-21 13:14:54

标签: javascript this

有时候,我遇到了一段像这样的javascript代码:

function someFunc() {
    var that = this;
    // do something with that
}

为什么人们在使用'this'时会这样做?

1 个答案:

答案 0 :(得分:1)

检查此示例:

function someFunc() {
  var that = this;
  $('.test').on('click', function() {
    //in this scope the this will be different and if you'd like to use the this of someFunc then you need to assign another variable
  })
}