binding of this when calling a function inside a function

Aaron

Was reading through MDN and came across this

function Person() {
  // The Person() constructor defines `this` as an instance of itself.
  this.age = 0;

  setInterval(function growUp() {
    // In non-strict mode, the growUp() function defines `this` 
    // as the global object, which is different from the `this`
    // defined by the Person() constructor.
    this.age++;
  }, 1000);
}

var p = new Person();

Why is it that this inside the function growUp() binds to window?

Dmitry Poroh

Because this is not bound to anything else. If it is not bound then it is bound when called to default object. Default is global window object.

If you need to bind it to person you can explicitly do it with bind function:

function Person() {
   this.age = 0;

  setInterval(function growUp() {
    this.age++;
  }.bind(this), 1000);
}

var p = new Person();

Edit:

In javascript you have two ways of calling function:

As function itself

function f() {}
f();

As property of the object

var obj = {
    f: function() {}
};
obj.f();

Via object prototype:

function X() {}
X.protoype.f = function() {}
var x = new X();
x.f()

For the first case this is not defined and need to be set to something. By default it is global object (window object in browser).

For the second and third case this is set to the object before dot '.' and they differs only how the function is found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When calling the function the initial state is not changed inside another function

Syntax error when calling function inside if statement in trigger function

Syntax Error when calling Recursive Function inside a Recursive Function in Ocaml

Calling a Function Inside Map Function

Calling a function inside a function in C

Calling a function inside JSX

calling a async function inside then

Calling a function inside itself

Calling a Function Inside a Loop

Calling subclass (virtual) function (late binding), when not dealing with pointers

Calling function inside function in another function

Which of the following shell operations are performed inside the function body when running a function definition and when calling a function?

When binding a event to two functions and unbinding it inside one function

404 error when binding named function with jQuery ajax inside

SUDO not found when calling inside a function in BASH script

" Uncaught (in promise) " when calling the reject function inside a fetch 'then' method

Preventing recursion when calling a builtin inside a shell function

Magento calls not happening when calling from inside a function

Call a function inside a class when calling the class in python

Calling an object function gets undefined when run inside a click event

What is the equivalent of not calling a callback when inside an async function?

`Task` blocks main thread when calling async function inside

Embedded Software: Exception occurs when calling function inside a task

strange behavior when calling python function inside if statement in batch file

Promise pending when calling a function inside a hook using React

No matching function when calling the function

How to fix is not a function error when calling a function i created inside of a new function i created

Jquery: Why is function returning global var when calling a function inside a function?

ReferenceError when calling function