강의

멘토링

커뮤니티

Programming

/

Programming Language

Life Coding - JavaScript Immutability

This is a JavaScript-related lecture provided by Life Coding. It is a class on how to handle data immutably in JavaScript.

(4.9) 100 reviews

2,789 learners

Level Basic

Course period Unlimited

  • Egoing Lee
JavaScript
JavaScript
JavaScript
JavaScript

Reviews from Early Learners

Reviews from Early Learners

4.9

5.0

장병훈

100% enrolled

Egoing you...

5.0

오영주

33% enrolled

egoing is the best!

5.0

김남연

100% enrolled

It's so easy to understand. It seems like essential content for data management.

What you will gain after the course

  • How to handle data immutably in JavaScript

  • Differences between how primitive data types and objects behave

  • How to clone an object

  • How to modify a clone without modifying the original object

1. Lecture Introduction

Life Coding's JavaScript Immutability course covers how to handle data immutably in JavaScript. Targeted at learners familiar with basic JavaScript syntax, this course explores how to prevent data corruption by making it immutable.

This course covers the following topics:

  • Differences between how primitive data types and objects behave
  • How to clone an object
  • How to modify a clone without modifying the original object

The following is not covered:

  • immutable library

2. Lecture target

Treating data immutably significantly reduces the likelihood of bugs caused by interference between data. It also makes it much easier to check whether data has changed. It also serves as the foundation for implementing advanced techniques like hot module reloading and time travel.

If you can predict the results of all the code below, you don't need to take this class.

var n1 = 1;
var n2 = 1;
console.log(n1 === n2);
=> It is true. Of course.

var o1 = {name:'kim'}
var o2 = {name:'kim'}
console.log(o1 === o2);
=> false . It's a bit ambiguous. JavaScript treats primitive data types, which don't change, differently from objects, which can.

var o1 = {name:'kim'}
var o2 = o1;
o2.name = 'lee';
console.log(o1.name);
=> This is lee. o1 doesn't even know English and the value of the name it is pointing to has changed.

var o1 = {name:'kim'}
var o2 = Object.assign({}, o1); // Copy o1 to an empty object.
o2.name = 'lee';
console.log(o1.name);
=> This is Kim. Changing o2 does not affect o1. This allows o1 to remain invariant with respect to o2.

var o1 = {score:[1,2]}
var o2 = Object.assign({}, o1);
o2.score.push(3);
console.log(o1.score)
=> [1,2,3]. I don't know English, so o1 changed again. This is because score is an array, which is a type of object.

var o1 = {score:[1,2]}
var o2 = Object.assign({}, o1);
o2.score = o2.score.concat(); // Copy the array.
o2.score.push(3);
console.log(o1.score)
=> [1,2].

There is another way: copy everything.
var o1 = {score:[1,2]}
var o2 = JSON.parse(JSON.stringify(o1));
o2.score.push(3);
console.log(o1.score)
=> [1,2].

It's a good idea to be careful not to change the original, but it's also possible to prevent the original from changing at all.
var o1 = {name:'kim'}
Object.freeze(o1);
o1.name = 'lee';
console.log(o1.name);
=> It's 'kim'.

But objects don't do this.
var o1 = {score:[1,2]}
Object.freeze(o1);
o1.score.push(3);
console.log(o1.score);
// [1,2,3].

Defensive freezing is required.
var o1 = {score:[1,2]}
Object.freeze(o1);
Object.freeze(o1.score);
o1.score.push(3);
console.log(o1.score);
// It doesn't change. It even causes a protest error.

3. Lecture participation conditions

To take this course, you need the following prerequisite knowledge:
(You can study either one.)

4. Total playback time

This is a one-hour class consisting of 12 videos.

5. Class Copyright

This lecture is licensed under a Creative Commons license and can be viewed at the link below.
https://opentutorials.org/module/4075 I would like to thank Life Coding for sharing valuable knowledge.

Recommended for
these people

Who is this course right for?

  • For those who want to handle data immutably and reduce the possibility of bugs

  • Anyone who wants to build a foundation for implementing advanced JS techniques

Need to know before starting?

  • JavaScript

  • NodeJS (optional)

Hello
This is

311,812

Learners

8,559

Reviews

1

Answers

4.9

Rating

44

Courses

Curriculum

All

12 lectures ∙ (1hr 0min)

Published: 
Last updated: 

Reviews

All

100 reviews

4.9

100 reviews

  • bhjang45440679님의 프로필 이미지
    bhjang45440679

    Reviews 1

    Average Rating 5.0

    5

    100% enrolled

    Egoing you...

    • lyc11211212487님의 프로필 이미지
      lyc11211212487

      Reviews 28

      Average Rating 4.3

      5

      33% enrolled

      • oyj님의 프로필 이미지
        oyj

        Reviews 4

        Average Rating 5.0

        5

        33% enrolled

        egoing is the best!

        • park7844323님의 프로필 이미지
          park7844323

          Reviews 5

          Average Rating 5.0

          5

          33% enrolled

          • nam002425274님의 프로필 이미지
            nam002425274

            Reviews 2

            Average Rating 5.0

            5

            100% enrolled

            It's so easy to understand. It seems like essential content for data management.

            Free

            Egoing Lee's other courses

            Check out other courses by the instructor!

            Similar courses

            Explore other courses in the same field!