Why does 'this' in a function display window object where as 'this' in an argument displays the object from the function was invoked?
Why does 'this' in a function display window object where as 'this' in an argument displays 开发者_高级运维the object from the function was invoked?
function show(x){
alert(x)//displays [object HTMLDivElement]
alert(this)//displays [object Window]
}
<div onClick="show(this)">123</div>
Because the function is defined in the global scope, so "this" will refer to the window object.
精彩评论