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.

Share Improve this question edited Mar 30, 2021 at 10:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 30, 2017 at 7:07 user5228393user5228393 8
  • 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 and this 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
 |  Show 3 more ments

2 Answers 2

Reset to default 2

In 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