Javascript
/
Types
- 1 Fundamentals 11
-
Hello World
-
Code structure
-
Use strict
-
Variables
-
Data types
-
Type conversions
-
Maths
-
Comparitions
-
Conditional
-
Loops
-
Functions
- 2 Testing 2
-
Mocha
-
Nested describe
- 3 Objects 4
-
Basics
-
Reference
-
Methods
-
Constructor
- 4 Types 5
-
Primitives
-
Numbers
-
Strings
-
Arrays
-
Json
- 5 Classes 3
-
Constructor
-
Binding
-
Inheritance
/
Primitives
➟
➟
Last update: 26-01-2022
Objects
! Objects are heavier than primitives.
/**
* One of the best things about objects is that ...
* we can store a function as one of its properties.
*
* But that come with a cost.
* Objects are heavier than primitives.
*/
let user = {
name: 'John',
message: () => console.log("Hello World")
}
user.message(); // Hello World
Wrappers
Object wrappers allow us to access primitives using methods.
/**
* The object wrappers are different for each primitive type:
* String, Number, Boolean, Symbol, BigInt
*
* 1) The s1 string is a primitive
* 2) the wrapper method is called and a String object is created
* 3) the Object is destroyed, leaving str as primitive
*
* Primitives can provide methods and still remain lightweight.
*/
let s1 = 'Hello World';
let s2 = s1.toUpperCase();
console.log(s2); // HELLO WORLD
let n1 = 1.23456;
let n2 = n1.toFixed(2);
console.log(n2); // 1.23
➥ Questions github Types