Warm tip: This article is reproduced from stackoverflow.com, please click
function javascript methods this

What does "With a regular function 'this' represents the object that calls the function" mean?

发布于 2020-03-28 23:15:53

In the explanation of arrow functions, w3schools says, "In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever" and "With a regular function this represents the object that calls the function". Link: https://www.w3schools.com/js/js_arrow_function.asp

I'm trying to understand this sentence, not in relation to arrow functions but by itself. It seems that many other sources I found on the net contradict to this, saying that when a function gets called on its own (not as a method), 'this' gets bound to either the toplevel object (e.g. window) or undefined, depending on strict mode.

So I built a simple example (running non-strict in the browser). I did not want to use the examples from the w3schools site because they use event handlers for explanation, and I want to make sure that I avoid any additional binding logic that event handlers introduce.

<script type="text/javascript">

function foo() {
    console.log('foo on ' + this.prop);
}

function bar() {
    console.log('bar on ' + this.prop);
    var f = this.foo;
    f();
}

var x = {
    prop: 'x'
};
window.prop = 'w';

x.foo = foo;
x.bar = bar;
x.bar();

</script>

This logs: "bar on x" and "foo on w", which seems to indicate that in foo, this refers to the window. So obviously the w3schools explanation does NOT refer to an automatic binding of foo's this to bar's this when foo gets called inside bar. But what else does it mean?

Questioner
Martin Geisse
Viewed
75
2020-01-31 17:57

Well, let's start with the premise that W3Schools has a less than stellar reputation and you shouldn't be thinking too hard about whatever it is they're writing. In fact, you should preferably read MDN instead.

In regular functions the this keyword represented the object that called the function...

Well, yeah, no… An object doesn't call anything. An object just is. It's a container of other stuff. One such "stuff" may be a function. You may then have code which takes the function from that "container" and calls it. So, code calls functions, whether those functions are a property of objects or otherwise.

How this is determined inside that function depends on exactly how that code calls the function. In a nutshell, in the expression a.b(), this inside b will be a. As a general rule, this inside a function will be whatever came before the . when it was called.

If there was no . before its call, e.g. just b(), there's no this (how that manifests exactly depends on the strict mode). It doesn't matter whether that function was originally part of an object or not; all that matters is the exact expression that was used to call it.

For all the gory details, you can see How does the “this” keyword work?.

The footnote here being that you can bind this on a function or pass another this explicitly using call or apply, and fat arrow functions don't follow this rule at all.