This article is a mirror article of machine translation, please click here to jump to the original article.

View: 8720|Reply: 1

[JavaScript] Basic types and reference types in js

[Copy link]
Posted on 9/25/2018 4:05:14 PM | | |
Basic Data Types:Value access, which allows you to manipulate the actual value saved in the variable. Base type values refer to simple data segments.

There are five basic data types:Undefined、Null、String、Number、Boolean。

Citation Type:When copying a variable of an object, it is the reference of the object that is being manipulated, but when adding properties to the object, it is the actual object that is being manipulated. Reference type values are objects that may be composed of multiple values.

There are several types of citations:object, Array, RegExp, Date, Function, special basic wrapper types (String, Number, Boolean), and monolithic built-in objects (Global, Math).

For the difference between base type values and reference type values:

Reference type values add properties and methods, while base type values cannot

When copying a variable value, the base type creates a new value on the variable object and copies it to the new variable. Since then,Any action of the two variables does not affect the other; The reference type is to copy a copy of the value stored in the variable object to the new variable, but the value of both variables points to an object stored in the heap, that is, they actually refer to the same object.Changing one variable affects the other

Referencing type values, using an array as an example:

Pass parameters: Pass by value, copy the value outside the function to the parameter inside the function (a local variable), when the local variable is operated, the change of the local variable will be reflected outside the function, but this will not affect the value outside the function.

Of course, it can be difficult to understand when using objects:

Obviously, the parameters are passed by value, so why can the created p instance also get the name attribute added in setName()?

Because obj and p refer to the same object, obj will access the same object by reference, even if it is passed by value. Take a look at the example below to make it clear.

Rewrite the obj object inside the function, at this time the obj object is referencing a local object, and the outer p is still the original reference, so it will not change.

Types of assays:

typeof:确定变量是字符串、数值、布尔值还是undefined的最佳工具。

instanceof: Determine whether it is an object type.







Previous:The easiest pit to step on in mysql Binlog
Next:js compares the difference between two arrays
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com