JavaScript for Absolute Beginners 7

In this tutorial, we’re going to implement a user dictionary using JavaScript.



Other tutorials of this series.

Notes for this tutorial:

Task for this tutorial:

  • Implement a user dictionary
  • The data is stored in a JSON file
  • Support CRUD operations

What is CRUD?

  • CRUD = Create, Retrieve, Update and Delete
  • CRUD applications are everywhere, e.g. blogs

JSON

  • JavaScript Object Notation
  • A data format for client-server communication
  • Often used in REST API
JSON
1
2
3
4
5
6
7
8
9
10
11
12
{
"name": "wilson",
"tutorial": 7,
"great": true,
"listOfNumbers": [
1,
2,
10,
5,
92
]
}

Plain JS Object

Plain JS Object
1
2
3
4
5
6
7
8
9
10
11
12
const foo = {
name: "wilson",
tutorial: 7,
great: true,
listOfNumbers: [
1,
2,
10,
5,
92
]
};

Dictionary

  • Key-value pairs
  • Very efficient if you want to retrieve a value of the specified key

Modules

Foo.js - Export a module
1
2
3
4
5
6
7
8
class Foo {
//...
bar() {
//...
}
}

module.exports = Foo;
main.js - import a module
1
2
const foo = require('./Foo');
foo.bar();

Using third-party library

  • NPM - Node Package Manager
  • Manage the library or dependencies in package.json
1
2
$npm init
$npm install someLib --save
Using the library
1
2
const someLib = require('someLib');
someLib.someFunc();

The source code of the program in this tutorial on GitHub.