Website lesson 7: understanding js
Welcome back! If you are a new user, I really advise you to read every previous post, starting from "Is HTML and CSS worth it?" . If you are still reading, it means you are ready to move to harder things.
Summing up
- Every element shouldn't be somewhere in the space, but in a structure: mass of structures.
- Every variable name has the most value. If you have to write a line about somebody's name, then your variable is called "name"
- Every function is kept in a class. Why? To structure our functions too. Like a mother with all her children - they all belong to her.
More about js
Hopefully, you’ve already checked the communities , I offered
Clear function
If you are not a beginner, you already know that in every language we have constructors and clear functions. Let's realize it:
clear(){
posts.splice(0,posts.length);
}
We define this function as a part of class, so it is visible for every element of our class.
What do we send to the function?
Nothing. We work just inside of our class, we don't need anything outside (user's info) to clear the database.
Template:
"what we delete".splice(from, how much);
Remove post
What to do, if we want to delete a specific one? Not every, but one with a specific id? The realization is similar to clear: you already know the method to delete smth - splice.
removePost(id){
for (var i = 0; i < posts.length; i++) {
if (posts[i].id === id) {
return posts.splice(i,1);
}
}
throw "No object with " + id + " id";
}
What do we send?
As the user decides which element to delete, we need a filter - id. We send id as it is the information from outside.
Then we check for id equal to find the object with the right id and then we use our method to delete.
Algorithm:
Check for the information from outside -> Find the object -> Delete it.
Add post
Again you want to add a specific object. First you have to understand where to add: to the beginning or to the end of your database. Normally, we count from one to ... .Logically, we add to the end.
addPost(Object){
if(this.validatePost(Object)){
posts.splice(posts.length,0,Object);
return true;
}
else {
return false;
}
}
Algorithms of functions always start from checking the information from outside. We can't add the object, until the user enters its data and sends it to our function.
Important thing: we check if all the fields are correct by using our validating function that I described in the previous lesson . Why? Everybody can make a mistake and put in the name some numbers instead of string values.
If yes, we add an object and return true.
Imitating communication
If we will check the adding function we need some object as it's the user's data. Let's initialize:
let ob={
id: '5',
destination: 'England',
createdAt: new Date(2018, 0, 1, 2, 3, 4, 567),
author: 'ZaicevAnatoliy',
tag: "spring",
flug: 'A-737'
}
To work with class during the communication, we also need an object of class:
let a = new Work(posts);
Testing Add Post
As always to make checking comfortable we output some messages.
console.log("test addPost: ")
console.log(a.addPost(ob))
The second line outputs the result of the addPost function. Now you see, what do we need an object of our class - to call its function outside the class. And what do we have in the brackets? That user's data (object) with all required fields to add.
Testing Remove Post
The output message + the result of our function
console.log("test removePost: ")
console.log(a.removePost("2"))
Consider! My functions aren't fully correct. You also have to check, if the id is out of the borders or if the user entered not a numeric value.
Testing clear function
Logically, we just call this function, using our object a
console.log("test clear: ")
console.log(a.clear())
As you understand our database will be empty. Then I guess you have a question: FOREVER??
No, you just imitate communication: nothing will be changed after.
Thanks for appreciation!
In 4 days we will end js functions, I will share my full code for it. Don't think that the end is near :)
It is not even half !
More info you can find on my website.
Good luck with your job!
Written by Yuri Filatov, IT Expert at Andersen