JavaScript PDF
JavaScript PDF
Pre-requisite:
--------------
1) Basic knowledge of JavaScript, HTML and CSS is required
2) ES6 new features.
3) Knowledge of Angular framework is advantage (optional)
Introduction:
-------------
1) It is a JavaScript library created by Facebook in 2011.
2) Every application is basically divided into 3 parts:
a) UI/Presentation Layer
b) Business Layer
c) Data Layer
3) React JS is a library used for creating UI.
4) It is used by Instagram and Netflix.
5) React - library - Only render the view and synch the
state of component.
6) Component - Main part of React JS.
7) A component is a piece of UI.
8) when we develop an application, we create a bunch of
independent isolated and reusable components, finally
combine all the components to create complex UI.
9) Used to create Single Page Applications (SPA)
10) We can build modern, fast Single Page Applications or
websites with React.
Q. What is a Library?
-- A library in programming can be explained as a collection
of codes. We use a library to write code in a much
simpler way or to import a feature from it into our
project.
-- For e.g. jQuery is a library.
-- We can write JavaScript much simpler by using jQuery, or
we can import written jQuery features to our project.
The project itself is not dependent on a library.
Q. What is a Framework?
-- A Framework, on the other hand, is a complete package of
code with its own functionalities & libraries.
-- A framework has its own rules, you don’t have much
flexibility and the project is dependent on the framework
you use.
-- Angular is an example of a framework.
Software required:
-------------------
1) NodeJS
2) Visual Studio Code.
OR
http://localhost:3000
Some concepts of JavaScript that you should know before learning React:
-----------------------------------------------------------------------
function sayHello() {
for(var i = 0; i < 5; i++) {
console.log(i);
}
}
sayHello();
function sayHello() {
for(var i = 0; i < 5; i++) {
console.log(i);
}
console.log(i)
}
sayHello();
var - If we define variable with the var keyword, then that variable
will be accessible inside the function in which it is defined.
function sayHello() {
for(let i = 0; i < 5; i++) {
console.log(i);
}
console.log(i); //will get an error here.
}
sayHello();
var -> scope - function
let -> scope - block
const x = 1;
x = 2; //error here x is read-only.
2) Objects:
-----------
const person = {
name: "Alex",
age: 34,
walk() {},
run: function() { }
}
console.log(person.name)
person.walk()
person.run()
console.log(person['age'])
3) this keyword:
----------------
const person = {
name: "Alex",
age: 31,
walk() {
console.log(this);
}
}
person.walk();
case 2:
-------
const person = {
name: "Alex",
age: 31,
walk() {
console.log(this);
}
}
4) bind() method:
-----------------
Complete Example
-----------------
const person = {
name: "Alex",
age: 31,
walk() {
console.log(this);
}
}
walk = person.walk.bind(person); //assigning reference of walk()
//function.
walk();
5) Arrow functions:
-------------------
const jobs = [
{id: 1, isActive: true},
{id: 2, isActive: true},
{id: 3, isActive: false}
]
-----------------------------------
Arrow functions and "this" keyword:
-----------------------------------
const person = {
name: "Alex",
age: 32,
walk() {
setTimeout(function() {
console.log("this", this);
}, 1000)
}
}
person.walk(); //output: window object.
How to solve this problem? How can I get reference to person object
in callback function?
--------------------------------------------------------------------
Old way:
--------
const person = {
name: "Alex",
age: 32,
walk() {
var self = this;
setTimeout(function() {
console.log("self", self); // this -> person object
}, 1000)
}
}
person.walk();
New way: use arrow functions
----------------------------
const person = {
name: "Alex",
age: 32,
walk() {
setTimeout(() => {
console.log("this", this); // this -> person object
}, 1000)
}
}
person.walk();
Let's summarize:
----------------
1) const person = {
name: "Alex",
age: 32,
walk() { // Regular function
console.log(this); // person object
}
}
2) const person = {
name: "Alex",
age: 32,
walk() {
setTimeout(function() {
console.log("this", this); // window object
}, 1000);
4) const person = {
name: "Alex",
age: 32,
walk() {
//inside walk(), this ----> person object
// person object [using arrow function]
5) const person = {
name: "Alex",
age: 32,
walk: () => {
console.log(this); //window object
}
}
person.walk();
6) Arrays.map() method:
-----------------------
• ES6 introduced new method Arrays.map()
• We will use this method in React to iterate List.
const names = [“Alex”, “Anna”, “Bob”, “Jason”];
const names2 = names.map(function(name) {
return “<li>” + name + “</li>”;
})
console.log(names2);
OR
7) Object DE structuring:
-------------------------
const address = {
street: "677 Tanglewood Road",
city: "Rolling Fork",
state: "Mississipi",
zipcode: 39159
}
Cloning of an array:
--------------------
const even_numbers_clone = […even_numbers]
9) classes:
-----------
const person1 = {
name: "Alex",
age: 31,
walk() {
console.log("walk");
}
}
const person2 = {
name: "Anna",
age: 31,
walk() {
console.log("walk");
}
}
Problems:
---------
• walk() method code is duplicated.
• What else if we want to modify walk() method, we have to
modify in every object.
Solution:
---------
• Create class (blueprint/template)
class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
walk() {
console.log("walk");
}
}
Create objects:
---------------
const person1 = new Person("Alex", 31);
const person2 = new Person("Anna", 34);
10) Inheritance:
----------------
11) modules:
------------
• Instead of writing all code inside one file, we split the
code into multiple files, this is called as "Modularity".
• Each file is called as "module".
• Let’s modularize our previous code.
person.js
----------
export class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
walk() {
console.log("walk");
}
}
teacher.js
----------
import { Person } from "./person.js";
main.js
-------
import { Teacher } from "./teacher.js";
teacher.js
----------
import { Person } from "./person.js";
main.js
-------
import { Teacher, promote } from "./teacher.js";
teach() {
console.log("teach");
}
}
main.js
-------
import Teacher, { promote } from "./teacher.js";
Note: A module can have at most one default export but can
multiple named exports.
JSX
Babel:
• A transpiler for JavaScript best known for its ability to
turn ES6 (the next version of JavaScript) into code that
runs in a browser.
• Created by an Australian developer named Sebastian
McKenzie
• Can handle all of the new syntax that ES6 brings, along
with built-in support for React’s JSX extensions
• Has the greatest level of compatibility with the ES6
specifications.
• Let’s you use virtually all new features of ES6, without
sacrificing backward compatibility for older browsers.
Understanding JSX: