This is the fourth day of my participation in the August Text Challenge.More challenges in August

This should be a very basic knowledge of JavaScript, but it is estimated that many small partners are simply brought over, to the interview. So I’m going to take my notes, and I’m going to give you a brief introduction to the original values and reference values, and I’m going to start. ECMAScript states that variables can contain two types of data. A primitive value is the simplest form of data, and a reference value is an object composed of multiple values. There are six primitive values: Undefined, Null, Boolean, Number, String, and Symbol. Manipulating a primitive value is manipulating the actual value of a stored variable. Reference values, on the other hand, are objects that are stored in memory, and since JavaScript can’t directly access memory locations, you can’t directly manipulate the memory space in which the object is located. When you manipulate an object, you’re actually manipulating a reference to the object, not the object itself.

What are dynamic properties

Dynamic attributes are attributes and methods that can be added, modified, or deleted dynamically after a reference value is created. Example:

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

console.log(obj.name); // "Sam"
Copy the code

Definition: First create a reference value object and store it in the variable obj. Second give obj a name attribute with the value “Sam”. Unless the property is modified/deleted, you can print or access the property directly. A raw value cannot have attributes, although no error is reported when attributes are applied to it. Example:

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

console.log(me.sex); // undefined
Copy the code

The initialization is literal, so giving me the sex attribute is useless, and it cannot be accessed. If the original value is initialized with the new keyword, JavaScript creates an Object instance. Example:

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

console.log(me.sex); / / "male"
Copy the code

The value of the copy

The original and reference values are also different when copied by variable. The original value is copied to a new variable, while the reference value is actually a pointer to the copied value. Example:

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

console.log(myName); // "Sam"
Copy the code

When myName is initialized as name, “Sam” will also be copied to myName, independent of name, which can be understood as myName is a copy of Name. The copy of the reference value actually copies a pointer to the object in the heap memory. The property operation on the copied variable is actually the copied object. The two variables actually refer to the same object. Example:

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

me.name = "Sam";

console.log(you.name); // "Sam"
Copy the code

Judge value type

We often use typeof to determine the typeof a variable, which is useful for determining the typeof the original value, but not so useful for determining the typeof the reference value, since typeof is returned object for both reference and null values. Often, we need to know exactly what type of object this is, hence the instanceof operator. Grammar:

result = variable instanceof constructor
Copy the code

Instanceof returns a Boolean value. Example:

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

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

console.log(pattern instanceof RegExp); 
// Is the variable pattern RegExp?
Copy the code

If you use instanceof to detect the original value, false will always be returned because the original value is not an object.

Write in the last

Writing is not easy, I hope I can get a “like” from you. If the article is useful to you, choose “bookmark”. If there are any mistakes or suggestions, please comment and correct them. Thank you. ❤ ️

Welcome to other articles

  • Internationalize your website with Vite2+Vue3 | August Update Challenge
  • Actual combat: The front-end interface request parameters are confused
  • Practice: Implement a Message component with Vue3
  • Actual combat: Vue3 Image components, incidentally support lazy loading
  • What updates does One Piece, vue.js 3.0 bring
  • An article digests the major new features of ES7, ES8, and ES9
  • Common problems and solutions for technical teams
  • Introduction to 10 common new features in ES6
  • Vue3 script Setup syntax is really cool