본문 바로가기

Programming/JavaScript

An High-Level Overview of JavaScript

High-level

Any computer program needs resources such as memory and CPU

LOW-LEVEL language such as C

-> Developer has to manage resources manually

vs. HIGH-LEVEL languages such as JS, Python

-> No need to manage. Everything happens automatically

Garbage-collected

Cleaning the memory so we don't have to

Interpreted or just-in-time compiled

Computer는 본래 zero or one만 이해할 수 있음 -> machine code

that's not practical to write

vs. human-readable javascript code

 

convert to machine code = compiling

happens inside the JavaScript engine

Multi-paradigm

Paradigm: An approach and mindset of structuring code, which will direct your coding style and technique

 

Three popular paradigms

1. Procedural programming

2. Object-oriented programming (OOP)

3. Functional programming (FP)

 

paradigm 종류

Imperative vs. Declarative

Prototype-based object-oriented

Prototype

e.g. Array

Array.prototype.push

Array.prototype.indexOf

 

This prototype contains all the array methods

 

Built from prototype

Our array inherits methods from prototype

const arr = [1, 2, 3];
arr.push(4);
const hasZero = arr.indexOf(0) > -1;

First-class functions

In a language with first-class functions, functions are simply treated as variables. We can pass them into other functions, and return them from functions.

-> Passing a function into another function as an argument

Dynamic

Dynamically-typed language:

- No data type definitions. Types becomes known at runtime

- Data type of variable is automatically changed

let x = 23;
let y = 19;
x = "Sunny";

Single-threaded

Non-blocking event loop

Concurrency model: how the JavaScript engine handles multiple tasks happening at the same time

Why do we need that?

-> JavaScript runs in one single thread, so it can only do one thing at a time

So what about a long-running task?

-> Sounds like it would block the single thread! However, we want non-blocking behavior!

How do we achieve that?

-> By using an event loop: takes long running tasks, executes them in the "background", and puts them back in the main thread once they are finished.