Detailed explanation of primitive values ​​and reference values ​​in ECMAScript

Detailed explanation of primitive values ​​and reference values ​​in ECMAScript

Preface

This should be a very basic JavaScript knowledge point, but I guess many friends just briefly mentioned it and don't know anything about it during the interview. Here, combined with my previous notes, I will briefly introduce the relevant knowledge of primitive values ​​and reference values. Let’s get started.

ECMAScript stipulates that variables can contain two data types, either primitive values ​​or reference values. Primitive values ​​are the simplest data structures, while reference values ​​are objects composed of multiple values.

There are six primitive values ​​in total: Undefined, Null, Boolean, Number, String, and Symbol. Operating the primitive value means operating the actual value in the storage variable.

The reference value is an object stored in the memory. Since JavaScript cannot directly access the memory location, it cannot directly operate on the memory space where the object is located. When you operate on an object, you are actually operating on the reference of the object, not the object itself.

What are dynamic properties?

Dynamic properties mean that after a reference value is created, its properties and methods can be dynamically added, modified, and deleted. Example:

let obj = new Object();
obj.name = "Sam";

console.log(obj.name); // "Sam"

Explanation: First, a reference value object is created and stored in the variable obj. Second, a name attribute is assigned to obj with the value "Sam". Unless this property is modified/deleted, you can print or access this property directly.

Primitive values ​​cannot have attributes, although no error is reported when attribute operations are performed on them. Example:

let me = "Sam";
me.sex = "male";

console.log(me.sex); // undefined

Here, literal initialization is used, so even if the sex attribute is assigned to me, it is useless and the attribute cannot be accessed.
If you initialize a primitive value using the new keyword, JavaScript creates an Object instance. Example:

let me = new String("Sam");
me.sex = "male";

console.log(me.sex); // "male"

Copying of values

Primitive values ​​and reference values ​​also differ when they are copied through variables. Primitive values ​​are copied to a new variable, while reference values ​​actually copy the pointer to the value. Example:

let name = "Sam";
let myName = name;

console.log(myName); // "Sam"

When myName is initialized to name, "Sam" will also be copied to myName, which is independent of name and does not interfere with each other. It can be understood that myName is a copy of name.

The copying of the reference value actually copies a pointer pointing to the object in the heap memory. The attribute operation on the copied variable is actually the operation on the copied object. The two variables actually refer to the same object. Example:

let me = new Object();
let you = me;

me.name = "Sam";

console.log(you.name); // "Sam"

Determine value type

We often use typeof to determine the type of a variable, which is useful for determining the type of primitive values, but it is not very useful for determining reference values, because for reference values ​​and null, typeof returns object.

Usually, we need to know exactly what type of object this is, so we have the instanceof operator. The syntax is:

result = variable instanceof constructor

instanceof returns a Boolean value. Example:

console.log(person instanceof Object);  
// Is the variable person an Object?

console.log(colors instanceof Array);   
// Is the variable colors an Array?

console.log(pattern instanceof RegExp); 
// Is variable pattern a RegExp?

If you use instanceof on a primitive value, it will always return false because primitive values ​​are not objects.

Summarize

This is the end of this article about original values ​​and reference values ​​in ECMAScript. For more information about original values ​​and reference values ​​in ECMAScript, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Linux unlink function and how to delete files

>>:  Detailed explanation of MySQL database binlog cleanup command

Blog    

Recommend

Specific example of MySQL multi-table query

1. Use the SELECT clause to query multiple tables...

Example of how to implement local fuzzy search function in front-end JavaScript

Table of contents 1. Project Prospects 2. Knowled...

How to define data examples in Vue

Preface In the development process, defining vari...

Example of using negative margin to achieve average layout in CSS

For evenly distributed layouts, we generally use ...

Vue image cropping component example code

Example: tip: This component is based on vue-crop...

HTML basics HTML structure

What is an HTML file? HTML stands for Hyper Text M...

How to use provide to implement state management in Vue3

Table of contents Preface How to implement Vuex f...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

Similar to HTML tags: strong and em, q, cite, blockquote

There are some tags in XHTML that have similar fu...

Uncommon but useful tags in Xhtml

Xhtml has many tags that are not commonly used but...

How to use the Linux basename command

01. Command Overview basename - strip directories...

Implementation of Single Div drawing techniques in CSS

You can often see articles about CSS drawing, suc...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...