‘var=this;’在JavaScript中的意思是什么?在我看到的JavaScript文件中:function Somefunction(){
var that = this;
... }声明的目的是什么?that并分配给this?
3 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
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 });});
$('#element').click(function(){ // this is a reference to the element clicked on var that = this; $('.elements').each(function(){ // this is a reference to the current element in the loop // that is still a reference to the element clicked on });});
this
that
this
.
that
clickedEl
.
添加回答
举报
0/150
提交
取消