Featured Post

Learn Python by Coding a Simple To-Do List in Python from Scratch

Image
Introduction This simple project in Python will not only teach you to build a simple ToDo List, but also assist you to learn basic Python concepts along the way! The basic concepts that you will be learning are about using the indefinite loop, that is, while True, how to stop the loop, if-elif-else loops, and also about creating a functional to do list in which the user is able to not only add items, but also, view items, update or edit them, remove items and also delete the entire list itself.  Please note this is Part 4 of my Python series. If you missed the algorithms and planning stages, check out Parts 1, 2, and 3 on my Substack! What We Want Our To do List to do Defining the Problem : Creating a storage space for storing the to do list grocery items that I want to shop. I want to make the list with a menu that shows a menu of choice with features such as Add, View, Edit, Remove, Delete and Exit options. So, I need a way to add items, view them, edit them, remove any of the it...

Use JavaScript Constructor To Create A New Object



JavaScript Constructor 

In JavaScript, a constructor gets called whenever an object is created using the keyword, new. The idea of using a constructor is to create a new object and then set values for any of the object properties that is already existing in the object. The constructor method is a special method for creating and initializing an object that is created with a class. 

The Task

 In this freeCodeCamp certification coding lesson, we need to finish the code for the given assignment. We need to use the class keyword and write a constructor to create the Vegetable class.  The Vegetable class allows us to create a vegetable object with a property of name that gets passed to the constructor. 

Key Points

As per requirement of the project, following points to be noted for doing this task: 
Vegetable should be a class with a defined constructor method. 
The class keyword should be used. 
Vegetable should be able to be instantiated. carrot.name should return carrot .


The Code

 
//change code below
class Vegetable {
constructor(name){
this.name = name;
}
}
//change code above
const carrot = new Vegetable("carrot");
console.log(carrot.name);


Steps


Created a class with a name of Vegetable that  has the properties of  the Vegetable object. 

The next step would be to put a constructor with a parameter of name into this object and set it to this.object. 

Now, when you log into the console, you will get the output as carrot, as this is the new vegetable that you created. 

Had you used a parameter of "cabbage" instead of "carrot" in the constructor, then, the console would have given you the output of cabbage instead of carrot. So, this is so cool!

Solution




Source code for checking your solution for this project:

//create a Book class

class Book{
  constructor(author){
    this._author = author;
  }
  //Create a getter to get writer
  get writer(){
    return this._author;
  }
  
//Create a setter to set the writer
  set writer(updatedAuthor){
    this._author = updatedAuthor;
  }
}

const novel = new Book('anonymous');

console.log(novel.writer);

novel.writer = 'newAuthor';
console.log(novel.writer);

//create a Thermostat class
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  };
  
// Create a getter to get temperature in Celsius 
  
  get temperature() {
    return (5 / 9* (this.fahrenheit - 32);
  }//Create a setter to set temperature in Celsius 
  set temperature(celsius) {
  ;  this.fahrenheit = (celsius * 9.0/ 5 + 32;
  };
};
const thermos = new Thermostat(76);
let temp = thermos.temperature;
thermos.temperature = 26;
temp = thermos.temperature;
console.log(thermos.temperature);

The console values for the three logs are respectively should be 
anonymous
newAuthor
26



Watch out for more such tutorials. If you have specific topics in mind that you wish to request as a tutorial,  please do let me know in the comment section below, and I will be too happy to oblige accordingly. 


Related Posts that may help you:




Comments

Popular Posts

Build A Random Quote Machine in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

Build A Calculator in React

Beginner's Guide On For Loop in Python

How to remember code in your learning phase

Top Free Online Websites to Learn Coding