Start with JS Objects

Summary:  Objects in js are so beautiful: we can copy them at will, change and delete one of their properties, etc. But remember the saying: "With great power comes great responsibility." (With great power comes great responsibility)

As a front-end developer, you will feel the power of the concept of Object in JS. We say "everything in JS is an object". The most core features, such as from String, to arrays, to browser APIs, the concept of objects is everywhere. Here you can learn about everything in JS Objects.

 

At the same time, with the strong rise of React, whether you have followed this framework or not, you must have heard of a concept - immutable data (immutable.js). What exactly is immutable data? This article will start with the source of JS - objects, so that you can gradually understand the important concepts in this functional programming.

 

Objects in JS are so beautiful: we can copy them at will, change and delete one of their properties, etc. But remember one sentence:

 

"With privilege comes greater responsibility."

(With great power comes great responsibility)

 

Indeed, there are too many concepts in JS Objects, and we must not use objects arbitrarily. Below, I will start with basic objects and talk about immutable data and everything about JS.

 

This article originated from Daniel Leite's article on the 16th of this month: Things you should know about Objects and Immutability in JavaScript, I translated it, rewrote the examples used, and made a lot of more extensions.

 

Mutability and sharing are the root of all evil

 

Immutable data is actually an important concept related to functional programming. In contrast, functional programming sees variability as the root of all evil. But why is there such a conclusion?

 

This problem may be encountered by many programmers. Actually, if your code logic is variable, it's not "politically wrong". For example, the array operation in JS will directly change the original array, of course, there is no problem. for example:

 

let arr = [1, 2, 3, 4, 5];

arr.splice(1, 1); // return [2];

console.log(arr); // [1, 3, 4, 5];

 

This is our commonly used "delete an item of an array" operation. Well, he has no problem at all.

 

The problem actually arises from "abusing" mutability, which can have "side effects" on your program. Don't worry about what "side effects" are, it's a functional programming concept.

​​​​​​​

Original link

 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326605710&siteId=291194637