Back to Home

Object methods, "this"

Objects are usually created to represent entities of the real world, like users, orders and so on: And, in the real world, a user can act: select something from the shopping cart, login, logout etc. Actions are represented in JavaScript by functions in properties.

Method examples

For a start, let’s teach the user to say hello: Here we’ve just used a Function Expression to create a function and assign it to the property user.sayHi of the object. Then we can call it as user.sayHi(). The user can now speak! A function that is a property of an object is called its method. So, here we’ve got a method sayHi of the object user. Of course, we could use a pre-declared function as a method, like this:

Method shorthand

There exists a shorter syntax for methods in an object literal: As demonstrated, we can omit “function” and just write sayHi(). To tell the truth, the notations are not fully identical. There are subtle differences related to object inheritance (to be covered later), but for now they do not matter. In almost all cases, the shorter syntax is preferred.

“this” in methods

It’s common that an object method needs to access the information stored in the object to do its job. For instance, the code inside user.sayHi() may need the name of the user. To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method. For instance: Here during the execution of user.sayHi(), the value of this will be user. Technically, it’s also possible to access the object without this, by referencing it via the outer variable: …But such code is unreliable. If we decide to copy user to another variable, e.g. admin = user and overwrite user with something else, then it will access the wrong object. That’s demonstrated below: If we used this.name instead of user.name inside the alert, then the code would work.

“this” is not bound

In JavaScript, keyword this behaves unlike most other programming languages. It can be used in any function, even if it’s not a method of an object. There’s no syntax error in the following example: The value of this is evaluated during the run-time, depending on the context. For instance, here the same function is assigned to two different objects and has different “this” in the calls: The rule is simple: if obj.f() is called, then this is obj during the call of f. So it’s either user or admin in the example above. function sayHi() { alert(this); sayHi(); // undefined

Arrow functions have no “this”

Arrow functions are special: they don’t have their “own” this. If we reference this from such a function, it’s taken from the outer “normal” function. For instance, here arrow() uses this from the outer user.sayHi() method: That’s a special feature of arrow functions, it’s useful when we actually do not want to have a separate this, but rather to take it from the outer context. Later in the chapter info:arrow-functions we’ll go more deeply into arrow functions.

Summary

let user = {
  name: "John",
  age: 30
};
Example:

Follow the lesson from Microsoft Web-Dev-For-Beginners course

Tags: object