PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

Object constructor

Multiple objects

The regular {...} syntax allows to create one object. But often we need to create many similar objects.

New

Any function can be run with new, and it will execute the shown algorithm. The capital first letter is a convention (to make it clear that can be run with new).
 
function User(name) {
  
    // this = {};  // implicitl

  this.name = name;

    // return this; // implicit
}

let user = new User("Jack");

console.log(user.name); // Jack
... 2 lines
 
Methods    (2/3)

Methods

We can add to this not only properties, but methods as well.
 
function User(name) {

  this.name = name;

  this.sayHi = function() {

    console.log( "My name is: " + this.name );

  };
}

let john = new User("John");

john.sayHi(); // My name is: John
... 3 lines
 
Task     (3/3)

Task

Define User and add name property. Hint: Use Object constructor
 
// your code here



// your code END

let user1 = new User("Jack");
let user2 = new User("Ana");

console.log(user1.name); // Jack
console.log(user2.name); // Ana

Questions    
Classes

        A B C D E F
🔔
1/2