Notes

this

Edit on GitHub

JavaScript
2 minutes

this

Depending on where and how a function is called, this can mean different things. this is all about the context.

this is the owner of the function, i.e. the object where the method is called.

this is used a lot in clousres where functions have independant variables defined in an enclosing scope.

this is the strangest thing you’ll come across in JS, but just remember that it is the object that owns the method being called.

4 Ways that this takes a value

  • In normal functions
  • Within methods on objects
  • Within an object that has been constructed (a new instance of a prototype)
  • A function invoked with .call, .apply or .bind

1.

In normal functions, this is in Global scope (window) if it hasn’t been explicitly defined.

2.

this by a method on an Object will always reference the object itself.

 1Portland = {
 2    bridges: 12
 3}
 4
 5function logBridges() {
 6    console.log(this.bridges);
 7}
 8
 9Portland.foo = logBridges;
10
11Portland.foo(); //12
12logBridges(); //undefined

3.

when we use a constructor function to create a new object, this is the new object we created, not the constructor function.

 1const City = function(name, state) {
 2    this.name = name || 'Portland';
 3    this.state = state || 'Oregon';
 4
 5    printDetails() {
 6        console.log(My city is "+ this.name +", and my state is " + this.state);
 7    }
 8}
 9portland = new City();
10seattle = new City('Seattle', 'Washington');
11
12portland.printDetails(); // My city is Portland, and my state is Oregon
13seattle.printDetails(); // My city is Seattle, and my state is Washington

You can run this above constructor function a 100 times and it’ll create new instances with different values (which you provided). this changes every time you create a new instance, that’s why it is highly replicable code.

Node and this

In node, “this” in a normal function results in being global to the MODULE, not to the application. You can read more here.

From the docs, “In browsers, the top-level scope is the global scope. That means that in browsers if you’re in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.”

Read More