开发者

Why does 'this' in a function display window object where as 'this' in an argument displays the object from the function was invoked?

开发者 https://www.devze.com 2023-04-09 01:50 出处:网络
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消