PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

Methods

In JavaScript actions are represented by functions in properties. A function that is the property of an object is called method.
 
let user = {
  name: "John",
  age: 30
};

user.sayHi = function() {
  console.log("Hello!");
};

user.sayHi(); // Hello!
... 3 lines
 
Shorter Syntax    (2/4)

Shorter syntax

There exists a shorter syntax for methods in an object literal:
 
let user = {

  sayHi() { // same as "sayHi: function()"

    console.log("Hello!");
  }
};

user.sayHi(); // Hello!
... 1 lines
 
This    (3/4)

This

The value of this is the object “before dot”, the one used to call the method.
 
let user = {

  name: "John",
  age: 30,

  sayHi() {

    console.log(this.name);
  }

};

user.sayHi(); // John
... 3 lines
 
Tasks    (4/4)

Tasks

Task 1

Add method sayHi to object.
 
let user = {

  name: "John",
  age: 30,

// your code here



// your code END

};

user.sayHi(); // John

Questions    
Constructor

        A B C D E F
🔔
1/2