admin管理员组文章数量:1394046
In the code below:
<!DOCTYPE html>
<html>
<script type="text/javascript">
function foo() {
console.log(this);
}
</script>
<body>
<button id="bar" onclick="foo()">Button</button>
<p id="demo"></p>
</body>
</html>
Why is it that when I say onclick=foo()
on my button and console.log(this)
in the foo function, the this
variable is pointing to the window
?
Since technically onclick=foo()
is defined on the button so when the onclick
function is called it should automatically set the value of this
to the button correct? I know how to fix this but I never understood why this is happening.
In the code below:
<!DOCTYPE html>
<html>
<script type="text/javascript">
function foo() {
console.log(this);
}
</script>
<body>
<button id="bar" onclick="foo()">Button</button>
<p id="demo"></p>
</body>
</html>
Why is it that when I say onclick=foo()
on my button and console.log(this)
in the foo function, the this
variable is pointing to the window
?
Since technically onclick=foo()
is defined on the button so when the onclick
function is called it should automatically set the value of this
to the button correct? I know how to fix this but I never understood why this is happening.
- 1 it should automatically set the value of this to the button correct? and I know how to fix this IMHO its design flaw but still make question off-topic – Satpal Commented Oct 30, 2017 at 7:09
-
Since if I do
onclick=foo(this)
somehow the correct context of this is passed which is the button. But in this case,this
is pointing to the button andthis
wasn't pointing to the button in the question above. – user5228393 Commented Oct 30, 2017 at 7:18 - 1 Technically, an inline event is defined globally, jus don't use inline event handlers. – Teemu Commented Oct 30, 2017 at 7:18
- @Teemu I don't understand. If it's in the element this should point to the element correct? – user5228393 Commented Oct 30, 2017 at 7:19
-
1
In the element you have an attribute only, there has to be a wrapper function in the deeper implementation . Notice, that
this
refers to the element within the onclick attribute. – Teemu Commented Oct 30, 2017 at 7:22
2 Answers
Reset to default 2In languages like Ruby or Java, this
(or in case of Ruby self
) will always point to the object in which your method is defined. So in Ruby if you are working on the foo
method inside the Bar
class, self
will always point to the object which is the instance of the Bar
class.
JavaScript works quite surprisingly here. Because in JavaScript function context is defined while calling the function, not while defining it! This is what can surprise many when ing to JS from different fields. Such late binding is a powerful mechanism which allows us to re-use loosely coupled functions in variety of contexts.
To answer your question, this
, when called from an onclick event handler is executed by the global context.
in my opinion it is because you did not pass your button to your function like this :
<input type="button" value="mybutton1" onclick="dosomething(this.value)">
while your code is like this:
<button id="bar" onclick="foo()">Button</button>
and the entry of your function is empty:
<script type="text/javascript">
function foo() {
console.log(this);
}
</script>
so you should pass this to your function like this:
<script type="text/javascript">
function foo(e) {
console.log(e);
}
</script>
and because the value of this is not set by the call, this will default to the global object , which is window in a browser.
for more info please check this out.
本文标签: javascriptThe value of this in onclick for a buttonStack Overflow
版权声明:本文标题:javascript - The value of this in onclick for a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744088024a2588896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论